如何在ItemTemplate中获取隐藏字段值

本文关键字:隐藏 字段 获取 ItemTemplate | 更新日期: 2023-09-27 18:12:26

只是想问我如何在repeater中找到隐藏字段因为我的问题是我有一个按钮,我想在ItemTemplate中获得关联的隐藏字段因为当我试图获得隐藏字段值时我得到空值

  <asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
            <ItemTemplate>
                <div class="resourcesResult">
                    <asp:HiddenField ID="hf_resID" runat="server" Value='<%#  Eval("Id") %>' />
                    <a href='<%#  Eval("pageID") %>'><%#  Eval("name") %></a>
                    <br />
                    <asp:Literal ID="litSummary" runat="server" Text='<%#  Eval("summary") %>'></asp:Literal>
                    <br />
 <%--                   <asp:Repeater ID="rp_tagsSkill" runat="server">
                        <ItemTemplate>
                            <h6>
                                <%#  Eval("Description") %>
                            </h6>
                        </ItemTemplate>
                    </asp:Repeater>--%>
                    <asp:Repeater ID="rp_tagsTopics" runat="server">
                        <ItemTemplate>
                            <h6>
                                <%#  Eval("Description") %>
                            </h6>
                        </ItemTemplate>
                    </asp:Repeater>
                    <div id="controls">
                        <asp:ImageButton ID="imgbtnBookmark" runat="server"   OnClick="imgbtnBookmark_Click" />
                        <asp:DropDownList ID="ddlGroup" runat="server" DataSourceID="SqlDS_Groups" DataTextField="name" DataValueField="id" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Value="-1">Select Group</asp:ListItem>

protected void imgbtnBookmark_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
    Bookmark bm = new Bookmark();
    HiddenField hiddenField = rptGroup.FindControl("hf_resID") as HiddenField;
    bm.UserID =  
    Guid.Parse(Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString());
    bm.Resoursce.ResourceID = Convert.ToInt32(hiddenField.Value);
    Bookmark.Insert(bm);
}

如何在ItemTemplate中获取隐藏字段值

试试这个,改变你的按钮标签传递id作为CommandArgument

<asp:ImageButton ID="imgbtnBookmark" runat="server"   
    OnClick="imgbtnBookmark_Click" 
    CommandArgument='<%#  Eval("Id") %>'/>

在你的按钮点击事件中你可以使用

访问id
bm.Resoursce.ResourceID = Convert.ToInt32(e.CommandArgument.ToString());

rptGroup.FindControl("hf_resID") as HiddenField;改为e.Item.FindControl("hf_resID")....

jquery

 function showid(dllval) {
            var ID = $(dllval).parent().parent().find('[id*="hiddenID"]').val();
alert(ID)
}
asp.net

<asp:DropDownList ID="ddl" runat="server" onclick="showid(this);" >
 </asp:DropDownList>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("ID")%>' />