将值绑定到hiddenfield

时间:2014-08-27 17:20:48

标签: c# asp.net

 <asp:HiddenField ID="hidapplyuid" runat="server" EnableViewState="true" />

                        <asp:TemplateField Visible="false">
                            <ItemTemplate>
                                <asp:Label ID="UID" CssClass="" runat="server" Text='<%#Eval("candUID")%>'></asp:Label>      
                                 <asp:Label ID="JobApplyUID" CssClass="" runat ="server" Text='<%#Eval("jobApplyUID")%>'></asp:Label>                         
                            </ItemTemplate>                            
                        </asp:TemplateField>   

需要将Job Apply UID绑定到cs页面中的隐藏字段...请帮帮我......

2 个答案:

答案 0 :(得分:1)

您需要将HiddenField放在TemplateField内,否则它不会被数据绑定,因为GridViewDetailsView等无法访问它

从此更改

<asp:HiddenField ID="hidapplyuid" runat="server" EnableViewState="true" />

<asp:TemplateField Visible="false">
    <ItemTemplate>
        <asp:Label ID="UID" CssClass="" runat="server" Text='<%#Eval("candUID")%>'></asp:Label>      
        <asp:Label ID="JobApplyUID" CssClass="" runat ="server" Text='<%#Eval("jobApplyUID")%>'></asp:Label>                         
    </ItemTemplate>                            
</asp:TemplateField>   

这样的事情。将其移至TemplateField某处,例如

<asp:TemplateField>
    <ItemTemplate>
        <%-- Move hidden variable inside of the template, else it cannot be databound --%>
        <asp:HiddenField ID="hidapplyuid" runat="server" Value='<%#Eval("jobApplyUID")%>' />
    </ItemTemplate>                            
</asp:TemplateField>
<asp:TemplateField Visible="false">
    <ItemTemplate>
        <asp:Label ID="UID" CssClass="" runat="server" Text='<%#Eval("candUID")%>'></asp:Label>      
        <asp:Label ID="JobApplyUID" CssClass="" runat ="server" Text='<%#Eval("jobApplyUID")%>'></asp:Label>                         
    </ItemTemplate>                            
</asp:TemplateField>   

答案 1 :(得分:0)

从gridview评估控件非常容易,请尝试以下演示。在项目中添加新的测试页面。让我们有ASPX页面,如下所示......

<div>
    Job Apply GUID : <asp:Label runat="server" ID="uxJobApplyUID"></asp:Label>
     <asp:GridView ID="uxGrid" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:TemplateField HeaderText="JobApply UID">
                <ItemTemplate>
                    <asp:HiddenField ID="uxHiddenJobApplyUID" runat="server" Value='<%#Eval("jobApplyUID") %>' />
                    <asp:Button ID="uxShowDetails" runat="server" Text="Show Data" OnClick="uxShowDetails_Click" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
</div>

后面的代码如下......

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("jobApplyUID", typeof(string));
        dt.Rows.Add(System.Guid.NewGuid().ToString());
        dt.Rows.Add(System.Guid.NewGuid().ToString());
        dt.Rows.Add(System.Guid.NewGuid().ToString());
        dt.Rows.Add(System.Guid.NewGuid().ToString());
        dt.Rows.Add(System.Guid.NewGuid().ToString());
        uxGrid.DataSource = dt;
        uxGrid.DataBind();
    }
}
protected void uxShowDetails_Click(object sender, EventArgs e)
{
    this.uxJobApplyUID.Text = (((sender as Button).NamingContainer as GridViewRow).FindControl("uxHiddenJobApplyUID") as HiddenField).Value;
}

如果您尝试这样做,您可以从gridview中的任何控件中获取值。