简体   繁体   中英

ASP.NET Repeater Control - Getting Hiddenfield value inside the repeater control

I have a hiddenfield inside a repeater control and a button outside the repeater control. below is the asp.code that i have.

 <asp:Repeater ID="rptAccordian" runat="server" OnItemDataBound="rptAccordian_ItemDataBound"> <ItemTemplate> <div class="s_panel"> <h1> <a href="#" data-content="Tool tip"><%# Eval("Name") %></a> </h1> <div> <p> <small><span style="font-family: 'Segoe UI'; font-weight: bold;">Category Objective: </span><span style="font-family: 'Segoe UI'"><%# Eval("Objective") %></span></small> </p> <p> <small><span style="font-family: 'Segoe UI'; font-weight: bold;">Category Riskscore: </span> <code><%# Eval("Score") %><span>%</span></code></small> </p> <p> <code> <img src="Content/img/add.png" /><asp:LinkButton ID="Add" runat="server">Add Question</asp:LinkButton> </code> </p> <asp:HiddenField ID="hdnCategoryID" runat="server" Value='<%# Bind("CategoryID") %>' /> </div> </ItemTemplate> </asp:Repeater> <div id="modalpopup"> <asp:Button ID="btnInsertQuestion" runat="server" Text="Save" OnClick="btnInsertQuestion_Click" /> </div> 

My Backend code is as follows.

protected void btnInsertQuestion_Click(object sender, EventArgs e)
{
    HiddenField hf = (HiddenField)rptAccordian.FindControl("hdnCategoryID");
    catID = Convert.ToInt16(hf.Value);
    Response.Write("ID is") + catID;
}

There are 13 repeaters and each repeater will have different CategoryID for it. I have a Link Button called Add inside each repeater and when I press that button I will have a modal popup open and it will have a button. On clicking that button I need to display the appropriate CategoryID which belongs to that repeater control in which I clicked the ADD link button.

However, the hiddenfield hf is showing as null and I'm not able to get the value of the hiddenfield of that accordion.

You have to get the repeater item to access the hiddenfield:

protected void btnInsertQuestion_Click(object sender, EventArgs e)
        {  
            for (int i = 0; i < rptAccordian.Items.Count; i++)
            {
                var item = rptAccordian.Items[i];

                var hf = item.FindControl("hdnCategoryID") as HiddenField;
                var val = hf.Value;
            }
        }   

UPDATED

protected void Add_Click(object sender, EventArgs e)
        {
            var lb = sender as LinkButton;
            var par = lb.Parent.FindControl("hdnCategoryID");

        }

I have acheived my answer using jQuery. I found the control hdnCategoryID and its value using jQuery and i assigned that value to a new hidden field and retrieved that value to my click event.

You can get that without using hiddenfield. You need to declare Add LinkButton in repeater like this.

<asp:LinkButton ID="Add" runat="server" CommandArgument = '<%# Bind("CategoryID")'%> OnClick = "Add_Click">Add Question</asp:LinkButton>

You have written code for btnInsertQuestion button click, where as you requested to get CategoryID in Add button click, so I am assuming your requirement is correct, but you have typed something else.

To get CategoryId in Add button click , you need to write like this.

protected void Add_Click(object sender, EventArgs e)
{
    //Get the reference of the clicked button.
    LinkButton button = (sender as LinkButton );

    //Get the command argument
    string cat_id = button.CommandArgument;
    // Type cast to int if application and use it.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM