Gridview hidden label value

G

Guest

Hello all,
I have a gridview, and a label inside the <Columns> and ItemTemplate. The
label is a hidden. I want to retrieve the value of this label on the
gridview_OnPageIndexChanged event, but i seem to get a nullexception. Can
anyone please tell me what iam doing wrong.

When i do the "view source" option i do see the values in the label.
thanks for all the help
 
M

Michael Nemtsev

Hello Smita,

Can u demonstrate how u do this?
Generally the process is
1) Open the page source code, and make sure that your hidden control exist
in code.
2) using the Page.FindControl Grid->Row->Item u can get access to your hidden
control

---
WBR, Michael Nemtsev [.NET/C# MVP].
My blog: http://spaces.live.com/laflour
Team blog: http://devkids.blogspot.com/

"The greatest danger for most of us is not that our aim is too high and we
miss it, but that it is too low and we reach it" (c) Michelangelo

S> Hello all,
S> I have a gridview, and a label inside the <Columns> and
S> ItemTemplate. The
S> label is a hidden. I want to retrieve the value of this label on the
S> gridview_OnPageIndexChanged event, but i seem to get a nullexception.
S> Can anyone please tell me what iam doing wrong.
S>
S> When i do the "view source" option i do see the values in the label.
S> thanks for all the help
S>
 
N

Nesster13

Hello all,
I have a gridview, and a label inside the <Columns> and ItemTemplate. The
label is a hidden. I want to retrieve the value of this label on the
gridview_OnPageIndexChanged event, but i seem to get a nullexception. Can
anyone please tell me what iam doing wrong.

When i do the "view source" option i do see the values in the label.
thanks for all the help

I am not sure if there is an event called OnpageIndexChanged for a
gridview or not but what I have below is for the
SelectedIndexChanged.

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="test2" runat="server"
Text='<%# Bind("id") %>' ></asp:Label>
<asp:Label ID="testId" runat="server"
Text='<%# Bind("type") %>' Visible="true" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Code Behind:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
Label test =
(Label)GridView1.SelectedRow.Cells[0].FindControl("testId");
Response.Write(test.Text);
}

You should be able to apply to your other gridview event. This is
assumed that there is a row selected on the gridview.
 
G

Guest

Thank you for all the quick response.. i got it working, mistake on my side
was that the label was declared outside on page load and hence the object
value was never set which got me a nullpointer exception.
-
S

Nesster13 said:
Hello all,
I have a gridview, and a label inside the <Columns> and ItemTemplate. The
label is a hidden. I want to retrieve the value of this label on the
gridview_OnPageIndexChanged event, but i seem to get a nullexception. Can
anyone please tell me what iam doing wrong.

When i do the "view source" option i do see the values in the label.
thanks for all the help

I am not sure if there is an event called OnpageIndexChanged for a
gridview or not but what I have below is for the
SelectedIndexChanged.

<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID"
DataSourceID="SqlDataSource1"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="true" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="test2" runat="server"
Text='<%# Bind("id") %>' ></asp:Label>
<asp:Label ID="testId" runat="server"
Text='<%# Bind("type") %>' Visible="true" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Code Behind:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs
e)
{
Label test =
(Label)GridView1.SelectedRow.Cells[0].FindControl("testId");
Response.Write(test.Text);
}

You should be able to apply to your other gridview event. This is
assumed that there is a row selected on the gridview.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top