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

时间:2013-05-07 18:25:02

标签: c# asp.net

我想问一下如何在转发器中找到隐藏字段因为我的问题我有按钮而我想在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);
}

3 个答案:

答案 0 :(得分:1)

试试这个,更改按钮标记,将id作为CommandArgument

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

在按钮点击事件中,您可以使用

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

答案 1 :(得分:0)

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

我希望link能为您提供帮助。

答案 2 :(得分:0)

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")%>' />