Click here to Skip to main content
15,902,791 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using Templatefield of Grigview to show image in imagebox, name in label control and Id in Label Control in iteam template. All work fine it is showing data as I required but I want to access value of Id in Label on code behind page. i.e when I try to accesss Label say lblID it is giving error Label not found gridview control is on .ascx page.

What I have tried:

I am using Templatefield of Grigview to show image in imagebox, name in label control and Id in Label Control in iteam template. All work fine it is showing data as I required but I want to access value of Id in Label on code behind page. i.e when I try to accesss Label say lblID it is giving error Label not found gridview control is on .ascx page.
Posted
Updated 10-Apr-16 23:50pm
v2

1 solution

There is code here that shows how you access controls inside of gridviews etc

Finding controls | The ASP.NET Forums[^]
 
Share this answer
 
Comments
Kishor-KW 11-Apr-16 6:39am    
i did this using following code
Label txt = (Label)GridViewUser1.Rows[0].FindControl("postLID");
string test = txt.Text;

but now problem is various data is bound to iteam template. when i used this it will give an last iteam template label value. I want the value from those which is currently access by user
F-ES Sitecore 11-Apr-16 7:26am    
How do you define what is "currently accessed by the user"?
Kishor-KW 11-Apr-16 8:05am    
i am using iteam template. so all the data from the database is bind with gridview.
which contain 1]image in imagecontrol 2]name in label control 3]Id in label control.

so it will access all this value in one row. if values are three then it will show three image control and label controls representing the values at perticular row in gridview.

now let us assume gridview having three rows. Let us called those three rows on output window as layers, so it will show three layers each contain one imagebox control and two label control.

when I use following code
Label txt = (Label)GridViewUser1.Rows[0].FindControl("postLID");
string test = txt.Text;

it will only show Id of label at zero th row i.e. zero th layer.
if i want to get Id of second row which i currently access by using button click event. how to do this?
F-ES Sitecore 11-Apr-16 8:50am    
Add a command event to the button

<asp:TemplateField>
<itemtemplate>
<asp:Button ID="ButtonSelect" CommandName="Select" CommandArgument="<%#Container.DataItemIndex %>" Text="Select" runat="server" OnCommand="ButtonSelect_Command" />
</itemtemplate>


And in the code-behind

protected void ButtonSelect_Command(object sender, CommandEventArgs e)
{
int index = 0;

if (int.TryParse(e.CommandArgument.ToString(), out index))
{
// process row
}
}
Kishor-KW 11-Apr-16 9:00am    
hi...thanx its working. please would you explain it what actually performed by
CommandName="Select" CommandArgument="<%#Container.DataItemIndex %>

and
int.TryParse(e.CommandArgument.ToString(), out index))

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