Getting Value of Hidden field in GridView??

I have read a gazillion walkthroughs on this but none of them seem to work for me?? I have the label below that is storing the primary key from a sql db:

<asp:TemplateField Visible="false">

<ItemTemplate>

<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />

</ItemTemplate>

</asp:TemplateField>

Can someone please show me how to access this value using c#?

I can use the code below to access any of the values that are being displayed but I cannot access the hidden label?

int index = Convert.ToInt32(e.RowIndex);

GridViewRow selectedRow = grid.Rows[index];

TableCell themeCell = selectedRow.Cells[0];

string themeName = themeCell.Text;

Any Help would be greatly appreciated...

 

0
dkirk
2/18/2008 7:46:56 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

10 Replies
1406 Views

Similar Articles

[PageSpeed] 29

try somnething like this:

GridView gv = new GridView();
Label lbl = gv.Rows[10].Cells[0].FindControl("idLabel") as Label;

if (lbl != null)
{
    string text = lbl.Text;
}

.....................................

or like this in your code:

Label lbl = grid.Rows[e.RowIndex].Cells[0].FindControl("idLabel") as Label;

if (lbl != null)
{
    string text = lbl.Text;
}

 


David Kiff
http://DavidKiff.co.uk

-- "Mark As Answer" If my reply helped you --
0
DavidKiff
2/18/2008 8:26:02 PM

Yes, and it will not work ever. You see a hidden field does not render.  You could go to the datasource

I always use datatable

Datatable dt = (DataTable)GridView1.DataSource;
// Then use your index variable

and read from datasource instead.

 

 


I am not anti social, am just not user friendly
0
aamador
2/18/2008 8:41:47 PM

This is a snippet of working code

Dim rw As GridViewRow = GridView1.Rows(e.RowIndex)
Dim dt As Data.DataTable = CType(GridView1.DataSource, Data.DataTable)


Dim old_prodid As String = dt.Rows(e.RowIndex).Item(1)
Dim old_prodname As String
= dt.Rows(e.RowIndex).Item(2)
Dim old_supid As String
= dt.Rows(e.RowIndex).Item(3)
Dim old_catid As String
= dt.Rows(e.RowIndex).Item(4)
Dim old_qpu As String
= dt.Rows(e.RowIndex).Item(5)
Dim old_up As String
= dt.Rows(e.RowIndex).Item(6)
Dim old_uis As String
= dt.Rows(e.RowIndex).Item(7)
Dim old_uoo As String
= dt.Rows(e.RowIndex).Item(8)
Dim old_rol As String
= dt.Rows(e.RowIndex).Item(9)
Dim old_dis As String = dt.Rows(e.RowIndex).Item(10)

Dim prodid As String = CType(rw.Cells(1).Controls(0), TextBox).Text
Dim prodname As String = CType(rw.Cells(2).Controls(0), TextBox).Text
Dim supid As String = CType(rw.Cells(3).Controls(0), TextBox).Text
Dim catid As String = CType(rw.Cells(4).Controls(0), TextBox).Text
Dim qpu As String = CType(rw.Cells(5).Controls(0), TextBox).Text
Dim up As String = CType(rw.Cells(6).Controls(0), TextBox).Text
Dim uis As String = CType(rw.Cells(7).Controls(0), TextBox).Text
Dim uoo As String = CType(rw.Cells(8).Controls(0), TextBox).Text
Dim rol As String = CType(rw.Cells(9).Controls(0), TextBox).Text
Dim dis As String = CType(rw.Cells(10).Controls(0), TextBox).Text

Read from the data source


I am not anti social, am just not user friendly
0
aamador
2/18/2008 8:50:45 PM

aamador:

Yes, and it will not work ever. You see a hidden field does not render.  You could go to the datasource

Hmm not sure you understand!!  Just because it doesnt render doesnt mean we cant access it ;)!  If you would like a sample application demonstating it working please ask Smile

 Cheers,


David Kiff
http://DavidKiff.co.uk

-- "Mark As Answer" If my reply helped you --
0
DavidKiff
2/18/2008 9:01:27 PM

aamador:

Dim rw As GridViewRow = GridView1.Rows(e.RowIndex)
Dim dt As Data.DataTable = CType(GridView1.DataSource, Data.DataTable)


Dim old_prodid As String = dt.Rows(e.RowIndex).Item(1)
Dim old_prodname As String
= dt.Rows(e.RowIndex).Item(2)
Dim old_supid As String
= dt.Rows(e.RowIndex).Item(3)
Dim old_catid As String
= dt.Rows(e.RowIndex).Item(4)
Dim old_qpu As String
= dt.Rows(e.RowIndex).Item(5)
Dim old_up As String
= dt.Rows(e.RowIndex).Item(6)
Dim old_uis As String
= dt.Rows(e.RowIndex).Item(7)
Dim old_uoo As String
= dt.Rows(e.RowIndex).Item(8)
Dim old_rol As String
= dt.Rows(e.RowIndex).Item(9)
Dim old_dis As String = dt.Rows(e.RowIndex).Item(10)

Dim prodid As String = CType(rw.Cells(1).Controls(0), TextBox).Text
Dim prodname As String = CType(rw.Cells(2).Controls(0), TextBox).Text
Dim supid As String = CType(rw.Cells(3).Controls(0), TextBox).Text
Dim catid As String = CType(rw.Cells(4).Controls(0), TextBox).Text
Dim qpu As String = CType(rw.Cells(5).Controls(0), TextBox).Text
Dim up As String = CType(rw.Cells(6).Controls(0), TextBox).Text
Dim uis As String = CType(rw.Cells(7).Controls(0), TextBox).Text
Dim uoo As String = CType(rw.Cells(8).Controls(0), TextBox).Text
Dim rol As String = CType(rw.Cells(9).Controls(0), TextBox).Text
Dim dis As String = CType(rw.Cells(10).Controls(0), TextBox).Text

 O dear!! enough said! Quite messy!!! lol


David Kiff
http://DavidKiff.co.uk

-- "Mark As Answer" If my reply helped you --
0
DavidKiff
2/18/2008 9:02:34 PM

Thanks for the responses, I ran into some other issues and I am going to have to drop back and punt. I think once I fix them, my value issue will go away. Thanks again.

1
dkirk
2/18/2008 9:10:03 PM

No problem, could you mark as answer on the posts that helped you!?

Good luck in your project,


David Kiff
http://DavidKiff.co.uk

-- "Mark As Answer" If my reply helped you --
0
DavidKiff
2/18/2008 9:15:14 PM

Could you not put the id into the DataKeyNames property and access it through GridView1.DataKeys(index).values("id")?

0
BGRhoades
2/18/2008 9:17:40 PM

dkirk :

I have read a gazillion walkthroughs on this but none of them seem to work for me?? I have the label below that is storing the primary key from a sql db:

<asp:TemplateField Visible="false">

<ItemTemplate>

<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>' />

</ItemTemplate>

</asp:TemplateField>

Can someone please show me how to access this value using c#?

I can use the code below to access any of the values that are being displayed but I cannot access the hidden label?

int index = Convert.ToInt32(e.RowIndex);

GridViewRow selectedRow = grid.Rows[index];

TableCell themeCell = selectedRow.Cells[0]; string themeName = themeCell.


If you are trying to access controls inside Template Fields then Use FindControl Method as suggested by David.. Please note that the code you have is only applicable for Bound Column Fields.. Other way to achieve that is accessing it in RowDataBound event of  Grid something like

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{
if (e.Row.RowType = DataControlRowType.DataRow)

{

Label lbl = (Label)e.Row.Cells[0].FindControl("Label1"); //Just change the index of the cell to whick your Label was reside in the Grid
string str = lbl.Text;

}

}


 


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
2/18/2008 10:00:36 PM

You may also refer to this link on How to access INVISIBLE Columns in Grid

http://www.koffeekoder.com/ArticleDetails.aspx?id=178_Access_GridView_Invisible_Columns 


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
2/18/2008 10:02:30 PM
Reply:

Similar Artilces:

using javascript getting value from textbox when edit button is clicked and both controls are present in gridview control
Hi All using javascript I want to get value  from textbox when edit button is clicked and both controls that is texbox and button are present in gridview control .Alok Hi aloksinha83, Please see this post: http://forums.asp.net/t/1069245.aspx Thanks,Qin Dian TangMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help....

Not getting values in hidden fields in a Gridview
Here's the gridview:   <asp:GridView id="gvGenerateSchedule" runat="server" Width="500px" AutoGenerateColumns="False" DataSourceID="AccessDataSource1" DataKeyNames="squadDetailID" Font-Size="Smaller" Font-Names="verdana" SelectedRowStyle-Font-Size="Smaller" SelectedRowStyle-Font-Names="verdana" HeaderStyle-Font-Size="Small" EditRowStyle-Font-Size="Smaller" EditRowStyle-Font-Names="verdana"...

Get Value from Hidden Column in Gridview control
I have managed to get the value from my hidden field by using the SelectedIndexChanged event and the following line of code:string keyValue = GridView1.SelectedDataKey.Value.ToString()I would however prefer to get access to the hidden column value on the GridView1_RowDataBound event, so that I can then evaluate each row against the hidden columns value and decide whether or not to display the edit button in each row. Can I do this a different way? You can use e.Row.DataItem("field name") in the RowDataBound event -  as long as the field is in your SQL statement doesn't m...

Need to get value from gridview hidden label field
Ok, I'm building a shopping cart in C# 2005. I use a gridview to show the products in the shoppingcart, productID, name, color and amount, in a hidden field I have colorID. I use the "delete" button in the gridview to allow users to remove a product from the shoppingcart. But to get the buissnies tire and Data Tire to work I need to supplie productID and ColorID for the product to remove. The productID is easy, but I can't get the ColorID out as a string. And could need some help with that. I just hope it's possible. Thanks in advance  On the delete event, Find ...

GridView child of GridView
I have a Parent GridView and when you click in the Edit one of the columns has a Template Field which has a Child GridView. When you click Edit on the Child GridView I have a Template Field that has a TextBox. How to I retreive this value of the TextBox? GridViewParent-- GridViewChild (Edit - Template Field containing this GridViewChild)----- TextBox inside Template Field in GridViewChild. Thank you.Peace in ChristMarco Napolihttp://www.ourlovingmother.org What you can do as a solution is to override the RowDataBound and try to find the child GridView control. Once you get a reference to...

How to get cell values when GridView is bound with Template Control Fields
Hi, I have following GridView <asp:GridView ID="gdvProcAdmin" runat="server" AutoGenerateColumns="false" AllowSorting="true"> <Columns> <asp:TemplateField HeaderText="ID" Visible="false"> <ItemTemplate> <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Category" SortExpression="Type"> <ItemTemplate> <asp:Lab...

How to get the value of a textbox that has been added to a template field in a gridview. VB.Net
I have a gridview that shows products, description, price. I have added a Buttonfield and a TemplateField that has a Textbox with the id QuantityTextBox.What I am trying to do is get the value of the Quantity TextBox in the RowCommand event of the gridview. I can get the price but need to get the quantity also. To get the price I used ItemPrice = Server.HtmlDecode(row.Cells(5).Text) My template field is cell 6 but the above routine does'nt work. Any help would be greatly appreciated.Dan5150www.5150creations.com Hiuse this: VB:  Protected Sub GridView1_RowCommand(ByVal ...

Gridview Hyperlink Field needs to include value from a previous gridview and a field value
Hi all Hope you can help, here is what I'm trying to do. I have a gridview on a page that contains a value (PracticeCode) I use this value as a parameter to filter an additional GridView (Gridview2). Now what I also want to do is take that value (PracticeCode) and use it in as part of the URL for a hyperlink field in GridView2 concatenatint it with the value of a Gridview field called Documentname so that: Hyperlink field URL Is "/store/" & PracticeCode & "/" and DocumentName Anyone know how to do this  regards chubsterit wasn't me, ...

How to get the value hidden control of user control in .aspx page
How to get the value hidden control of user control in .aspx page   Thanks srinivas  If its a server control (runat attribute = server) and its markup starts with <asp:HiddenField ID="HiddenField1">, you can just call HiddenField1.Value to get the value stored in it. Even if your hidden field is not a standard asp.net control you can still retrieve the value provided the runat attribute is set to server.Hope this helps programming blog Use <%=testhid.ClientId%>For example getting a reference to the object would look like:var testid = document...

How to add values to template control fields textbox control in GridView..
Hi all,    <asp:GridView ID="GridView1" EnableViewState="true" DataKeyNames="LicenseTemplateID"                       runat="server" AutoGenerateColumns="False" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"                       Width="345px">                       <Columns>                           <asp:Templa...

Issue with getting values from child controls in a gridview, to use for the update using a SQLDataSource control
Hi all, I have a gridview bound with a SQLDataSource. I am using the Update feature of the SQLDataSource to update a SQL Server database with values entered into the gridview. However I am not getting it to work. I believe this is due to the controls that contain the user entries are not the gridview itself, but rather child controls within the gridview. I have been using the names of the actual controls but nothing happens. Upon submit, the screen returns blank, and the database is not updated. Here is some code:   <asp:GridView ID="GridEditSettlement" runat="server" AutoGenerate...

How to get the value out of a field from the gridview
Hi, I have a gridview with 2 bound fields, 1 template field with a textbox, 1 template field with a dropdownlist. I need to pass that 1 bound field, textbox template field, and dropdownlist template fields as parameters to a procedure (updating or whatever it needs to do). So, how can I obtain the values out of them for the row that I just edited to become parameters to pass it to a procedure? I am using VS2005 C#. I can get the values if they were in the edit mode, but not AFTER (when gridview1.editindex = -1). Any help/input would be appreciated. Thanks.   ...

how do i get to the value of a hidden field???
I have a lttle app and i am trying to get to the value of ahiden field on the form thru javascript and i am not expert and much less. i have thisfunction TryTest(str) {Objt = document.getElementById("hid1"); } i also tryed it like this: Objt = window.document.getElementById("hid1"); and like that: Objt =  $find('hid1') but i cant seem to be able to get a Value property from my objt object in my code. What is wrong. Is it because i installed AJAX toolkit? document.getElementById("hid1").value Regards Mike [MVP - ASP/ASP.NET]My site If...

get the valu of hidden field
hi I am using the following code to get the value of hidden field from javascript function. document.getElementById('hidField').value This statement is working fine for Internet Explorere.When i try with netscape navigator it is not working. If anyvbody have clue pls help me. Thanks RaviRP Ravi, What version of Netscape is this? Peter BrunoneMS MVP, ASP.NETFounder, EasyListBox.comDo the impossible, and go home early. Hi peterbrunone I am using NN 7.0.This issue is resolved.Problem is i am passing name to getelementbyid methos.this is working fine with IE but...

Web resources about - Getting Value of Hidden field in GridView?? - asp.net.presentation-controls

GridView SelectedItem Binding Gotcha in WinRT
Last night when I was doing some WinRT hacking I was trying to setup databinding to the selected item in the GridView control and I ran into ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Advanced Search Gridview Latest News Releases All ...

CNW Group
... Services Beyond the Wire About CNW my CNW CNW Access CNW Directories Financials Webcasts Events my CNW All News Releases Advanced Search Gridview ...

CNW Group
Home Services Beyond the Wire About CNW Financials Webcasts Events My CNW All News Releases Add a Filter Gridview Latest News Releases All Today ...

New CmdLets of Powershell 2.0
New CmdLets of Powershell 2.0

Price Drop: Softbox
... objects- create product reflection- changing the background with something more interestingUse it as:- softbox- honeycomb softbox- gridview ...

Profile.aspx: ASP Alliance
.NET,ASP,ASP.NET,SQL,XML,HTML,ADO,ADO.NET,JavaScript,JScript,CSharp,VBScript and VB tutorials from AspAlliance.

Quick Answers - CodeProject
Questions - Free source code and tutorials for Software developers and Architects.; Updated: 25 Oct 2012

DotNetKicks.com - Upcoming stories
DotNetKicks.com is a community based news site edited by our members. It specialises in .NET development techniques, technologies and tools including ...

View Features - ExtendASP
Call 847-681-9827 or Request a Demo View Features Testimonials Pricing About Us Coming Soon ExtendASP exposes every feature, more than any other ...

Resources last updated: 11/28/2015 10:39:14 PM