Click here to Skip to main content
16,011,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

I want to find control of a linkbutton which is in the itemtemplate and I used the following code and i get the value but because of using the for loop I am getting only the last value and the previous values is getting cleared here is my code pl tell me how to get the correct value

C#
<asp:TemplateField HeaderText="Associate ID">
                            <ItemTemplate>
                                <asp:LinkButton ID="lnkAssId" Text='<%# Eval("associate_id") %>' runat="server" 
                                    onclick="lnkAssId_Click">LinkButton</asp:LinkButton>
                            </ItemTemplate>
</asp:TemplateField>

C#
for (int i = 0; i < grdApproval.Rows.Count; i++)
        {
            LinkButton lblAssId = (LinkButton)grdApproval.Rows[i].FindControl("lnkAssId");
            Session["AssId"] = lblAssId.Text;
        }
Posted

1 solution

The reason that you are getting the last value because you are updating/overwriting the lblAssId.Text in your loop.

You can solve this issue either by adding each value to a list and display the values from the list. Or add the last label value to the existing Session.

Like

C#
Session["AssId"] = Session["AssId"].ToString() + " " + lblAssId.Text;

Or
C#
List<string> AssIds = new List<string>();
for (int i = 0; i < grdApproval.Rows.Count; i++)
{
  LinkButton lblAssId = (LinkButton)grdApproval.Rows[i].FindControl("lnkAssId");
  AssIds.Add(lblAssId.Text);
}
Sesion["AssId"] = AssIds;


Then write all the values in the list with a foreach or for loop.

Good luck,
OI
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900