Question

I'm having trouble with setting a HiddenField's value to a GridView item value. What I want to do is get the value of a BoundField (in this case, "FIPSCountyCode") in a GridView and store it in a HiddenField when the user clicks a button (in this case, "btnEdit") to make changes to a entry in the grid. I haven't used HiddenFields before, so I have forgotten what to do here.

The HiddenField is setup like this:

<asp:HiddenField ID="hdnFIPS"  Value='<%#Eval("FIPSCountyCode")%>' runat="server" />

This is what the GridView is setup like:

<asp:GridView ID="CountyList" runat="server" AutoGenerateColumns="False" Width="90%" SkinId="PagedList" PagerSettings-Position="TopAndBottom" PagerStyle-Wrap="True">            
    <Columns>
        <asp:BoundField HeaderText="County Code" DataField="FIPSCountyCode" />
        <asp:BoundField HeaderText="State Code" DataField="StateCode" />
        <asp:BoundField HeaderText="County Name" DataField="CountyName" />
        <asp:BoundField HeaderText="Tax Rate" DataField="TaxRate" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:ImageButton ID="btnEdit" runat="server" SkinID="EditIcon" OnClick="EditInfo" CommandName="DoEdit" />
                <asp:ImageButton ID="DeleteButton" runat="server" SkinID="DeleteIcon" CommandName="DoDelete" 
                                 OnClientClick="return confirm('Are you sure you want to remove this item and all of its options?')" 
                                 CausesValidation="false"  /> 
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle CssClass="even" />
</asp:GridView>

And this is the code behind:

public partial class Admin_County_Info : CommerceBuilder.UI.AbleCommerceAdminPage
{
    private string redirectString = String.Empty;

    protected void Page_Load(object sender, System.EventArgs e)
    {
        if (!Page.IsPostBack)
            PopulateCountyGrid();
    }

    protected void PopulateCountyGrid()
    {
        try
        {
            System.Data.SqlClient.SqlDataReader dr = null;
            using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
            {
                SqlCommand cmd = new SqlCommand("SELECT [FIPSCountyCode], [StateCode], [CountyName], [TaxRate] FROM [baird_InfoCounty]", cn);
                cmd.CommandType = CommandType.Text;
                cn.Open();
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                CountyList.DataSource = dr;
                CountyList.DataBind();
            }
        }
        catch (Exception eX)
        {

        }
    }

    #region Clicks and Event Handlers

    protected void EditInfo(object sender, System.EventArgs e)
    {
        if (Page.IsValid)
        {

            redirectString = "~/Admin/Taxes/AddEditCountyInfo.aspx?FIPSCountyCode=" + hdnFIPS.Value;
            Response.Redirect(redirectString);
        }

    }

    protected void AddInfo(object sender, System.EventArgs e)
    {
        if (Page.IsValid)
        {

            Response.Redirect("~/Admin/Taxes/AddEditCountyInfo.aspx");
        }

    }
}

This must be a really dumb question but I'm really not sure how to proceed. Any help would be great!

Était-ce utile?

La solution

You can get the value of FIPSCountyCode from the BoundField this way:

protected void EditInfo(object sender, System.EventArgs e)
{
    if (Page.IsValid)
    {
        GridViewRow gvr = ((ImageButton)sender).NamingContainer as GridViewRow;
        hdnFIPS.Value = gvr.Cells[0].Text;

        redirectString = "~/Admin/Taxes/AddEditCountyInfo.aspx?FIPSCountyCode=" + hdnFIPS.Value;
        Response.Redirect(redirectString);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top