0

我在将 HiddenField 的值设置为 GridView 项值时遇到问题。我想要做的是在 GridView 中获取 BoundField 的值(在本例中为“FIPSCountyCode”),并在用户单击按钮(在本例中为“btnEdit”)以进行更改时将其存储在 HiddenField 中网格中的条目。之前没用过HiddenFields,所以忘记了这里要做什么。

HiddenField 的设置如下:

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

这就是 GridView 的设置方式:

<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>

这是背后的代码:

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");
        }

    }
}

这一定是一个非常愚蠢的问题,但我真的不确定如何继续。任何帮助都会很棒!

4

1 回答 1

1

FIPSCountyCode您可以通过以下方式从 BoundField获取值:

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);
    }
}
于 2014-02-05T17:57:10.750 回答