【问题标题】:Storing BoundField Value in HiddenField on Button Click在按钮单击时将 BoundField 值存储在 HiddenField 中
【发布时间】:2014-02-05 17:36:29
【问题描述】:

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

    }
}

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

【问题讨论】:

  • hdnFIPSCountyList 之外吗?
  • 是的,它在 CountryList 之外。

标签: c# asp.net gridview boundfield hiddenfield


【解决方案1】:

您可以通过这种方式从BoundField 中获取FIPSCountyCode 的值:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 2022-09-30
    相关资源
    最近更新 更多