How to access GridView hidden Column value by clicking a template column button?

Hi:

My GridView contains(for simplicity sake) three columns: a hiden template column that contains the DocID (the DataKey), a template column that contains DocTitle and a template button column that contains a button called btnDetails.

When the GridView is loaded with data, the user may click the button on a row and obtain the DocID from the hidden column of that row.

This seems to be simple enough. However, when the button is clicked, the GridView always returns blank.

Any idea why this happens?

My code is as follows: 

ASPX <asp:GridView ID="gvList" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="DocID" ………………><Columns> <asp:TemplateField HeaderText="DocID" SortExpression="DocID" Visible="False">    <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("DocID") %>'></asp:Label> </EditItemTemplate>    <ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("DocID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Title" SortExpression="DocTitle"> <EditItemTemplate>    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("DocTitle") %>'></asp:TextBox> </EditItemTemplate>    <ControlStyle Width="220px" />    <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("DocTitle") %>'></asp:Label> </ItemTemplate>    </asp:TemplateField> <asp:TemplateField ShowHeader="False">    <ItemTemplate><asp:Button ID="btnDetails" runat="server" CausesValidation="false" CommandName="Select" Text="Details"  OnClick="btnDetails_Click" …………. /> </ItemTemplate>    </asp:TemplateField> </Columns></asp:GridView>                Code Behindprotected void btnDetails_Click(object sender, EventArgs e){String id;Button btndetails = (Button)sender;GridViewRow row = (GridViewRow)btndetails.Parent.Parent;      id = row.Cells[0].Text.ToString();

}

Please help. Thanks in advance.

 sg2000

 

0
sg2000
4/9/2008 5:40:50 PM
asp.net.presentation-controls 72751 articles. 3 followers. Follow

2 Replies
2503 Views

Similar Articles

[PageSpeed] 57

With the Gridview rather than using  Button_click events it is better to use one of the GridView events.

As your button has CommandName="select" use the SelectedIndexChanged event or for custom Commandnames use the RowCommand event.

In the SelectedIndexChanged you can easily get the value by gvList.SelectedValue or gvList.SelectedRow.Cells[0].Text

Tim

0
cashmore
4/9/2008 9:05:03 PM

Thanks very much, cashmore, for the quick response and valuable suggestion.

Yes, I abandon the button-clicked event approach and use the SelectIndexChanged event as you suggested. However, I use 

                      id = gvList.SelectedDataKey.Value.ToString();

to get the DataKey value from the hidden column. It works perfectly now. Thanks again.

sg2000

0
sg2000
4/10/2008 7:15:58 AM
Reply:

Similar Artilces:

Update GridView Column Content on click of Button which is included as a column in gridview
 I have Gridview wich has 4 columns1.Name of the Person2.Card Amount3.Template Field of TextBox4. Amount After Transfer I want user to enter the amount in the textbox which gets updated as the total amount (Card Amount + Amount Entered in TextBox) in the 4th column of "Amount After Transfer".I hv tried update Panel and Dynamic Populate bt nothing is working coz the textBox doesnt provide any event function in the code behing page.and javascript function doesnt allow me to refer to any control within the gridview.........PLZ HELP ME...............Thanx   You just place a...

How to determine the row number when clicking a delete button I added to a template column from a GridView control???
Hello all,I am working using the codebehinde the page with VB.NET 2005 (web application).  I have a gridview where I added a template column, inside that templated column I added a delete button.  I would like to know how could I determine which row number corresponds to the clicked button so I can delete the current record.This is what I have: Protected Sub LinkButtonDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Try Dim cmd As SqlCommand Dim objConnection As SqlConnection objConnection = New SqlConnecti...

accessing hidden column value in GridView
Hi, I have a GridView and SqlDataSource controls on a page.  The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which - "Modified" of type Date - is hidden since it should not be edited by a user.  The system handles the data for this column.  So, I have hidden (Visible=false) this column on the grid.  In order to access the value in this field, I have created a template column and assigned the "Modified" column to it. The Update Stored Procedure does the cocurrency checks by looking at the value i...

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....

remove a gridview column dynamically, removes controls in template columns
I have a interface with a treeview and a gridview. The gridview has two templatefields that handles insert/update/deletes. The gridview is handling editing of different tables so I create the other columns in the gridview dynamically as boundfields. The user selects the table to edit in the treeview. The problem I am having happens when the user selects a table to edit the second time. The code that executes removes all bound fields, (dynamically added when user selected the first table in the treeview), but not the two template fields, and then adds boundfields specific for the selected table. The problem (bug ?) is that this removes all controls (Imagebuttons) in the template fields. So, to reproduce, just remove a column in i gridview with template fields. This will remove all controls in all the template fields. (I haven't tried the result if removing predefined columns) If I use commandfield's instead of templete fields the code works fine. But... I don't want to do that. Greatful for every suggestion. Hi:   Please paste a demo that can reproduce your problem. ThanksSincerely,Allen ChenMicrosoft Online Community SupportPlease remember to mark the replies as answers if they help and unmark them if they provide no help. Thanks for the reply.  I will paste how the gridview looks in the aspx. The gridview is really in some divs, (to handle x-y scrolling) and a updatepanel and the gridview really has...

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...

Iwant to select a row and column in gridview with select radiobutton. but No checked change event.below put a button .if click that button to seleced row values will select
I want to select a row and column in gridview using radiobutton in gridview . if which radio button is select that row will select and i want that row column values in outside individual textboxes of next page.  but in that only single radiobutton will select and no post back="true" bcz iam not using checked change property. i inserted button below gridview. if i click the that selected row and columns values are stored into textboxes of next page im using this code .but im getting one column in that row. but i want all columns in that selected row so plz repla...

Issue with Template Columns & Bound Columns while updating the data in a GridView control
Hi, I'm facing the below problem (In general, ObjectDataSource is driving me crazy ! ) I have a simple gridview control and an ObjectDataSource is used to populate the grid. I'm trying to update a row and the corresponding tableadapter methods & stored procedure are in place. Each row has 2 checkboxes and 2 textboxes in edit mode. When the change the values in edit mode and hit update, the checkbox values changed are saved into the Database properly, whereas the textbox values are not being saved. If the textbox column is a bound column then the column is updated with <NULL> in Database. If I change it to a template column, then no update is done; the old value is retained in Database. So, in none of the cases I get the desired result. What am I missing here? I have NO code in any of the events like GridView_RowUpdating etc.. and I intend not to have any. This is pretty straightforward update. Please help.  Thanks Jai Hi Jai, Since there are only 2 columns updated, I suggest you check the UPDATE statement in your Update method of ObjectDataSource. The code to update the other 2 columns might be missing. You can try to add some breakpoints in the Update method, step through it to see if every parameter and value has been added properly. If you're using a SQL Server database, you can also start SQL Profiler to get a trace to see what is actually being executed on the server.Sincerely,Kevin YuMicrosoft Online Community SupportPlease remember to cli...

Accessing the contents of a template column dropdown list to populate other columns on a row in gridview
I have been searching for a couple of hours now and cannot find an answer to this.I have a web page in vs 2005 using vb.net.This has a grid on it which contains template columns.When I edit a row, I need to be able to access the value from a drop down list in the template column and populate one or more columns on the same row - for instance, the drop down allows me to select an Item from a price book.  When the item is selected I need to get its index from dropdown.selectedvalue, perform a lookup from the database, and populate the next column with the Item's units, and the column afte...

Accessing Gridview Cell Values from inside a Template column
I have a checkbox inside a template field of a gridview, and I need it to respond to a click by appending the key value of the row to a sql statement and executing it.  I have tried using dim ID as string = MyGrid.SelectedValue.ToString in the checkbox's OnClick event, but evidently, SelectedValue is populated only if a row is selected using the built-in "Select" button. I have been advised to add a custom attribute to the checkbox in the GridView's RowDataBound event.  In this context, I can get the value I need in GridView.DataKeys(e.Row.RowIndex).Value.ToString, but I can fi...

Get value from current Gridview column on button click
 I am searching for the answer to this, should be easy, but I just can't get it.I have a Gridview.  I placed a button column on it.  Now each row has a button.  When they click the button I want to grab the value of one of the columns and do some stuff.  I made it a template field with a button, on in the on_click of that button I put:myString = me.GridView1.SelectedRow.Cells("mycolumn").textThat said it was a nullRefernceException.  I know there is data in that column.  So I tried myString = me.GridView1.SelectedRow.Cells(1).textGot the same thing.What am I missing?Thank You  Hi,  You should use the RowCommand event of the GridView Control to get the cell's text.eg. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { Response.Write(GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[1].Text); } Hope this helps.Best Regards,RajaMCAD .Net, MCDBAPlease choose "Mark as Answer" for the post that leads you to a solution Bind the data item to the button's CommandArgument field something like this: <asp:TemplateField> <ItemTemplate> <asp:Button runat="server" CommandName="DoSomething" CommandArgument='<%#Eval("ID")%>' Text="DoSomething" /> </ItemTemplate></asp:TemplateField> Then in your RowCommand event handler you will have access to the data through th...

Get a Clicked Column Index Integer value for a GridView column using the Sorting Event Handler
I am creating a GridView nested within a UpdatePanel and I got the sorting, paging all down to a sciene using the GridView events handlers. Now, I stumbled to a minor problem that I can't seem to figure out how to go about it. When sorting, I would like the heading color of the clicked sorted column to change (using CSS). I can't seem to dynamically pass the value of the clicked column index to the method. I am using the following reference to change the color of the column but I cannot seem to retreive the value of the clicked index in integer value: GridView1.Columns(trying to...

Help-Get Value in GridView Column from click of linkbutton in Item Template?
Something so simple seems out of my grasp... I am using Visual Web Developer Express, ASP.Net 2.0 (VB Syntax).  I have a bound GridView.  I added a linkbutton control (lnk_Recipients) to the last column in the Item Template.  lnk_Recipients text value is set to Edit.  I want to extract one or more values from gridview columns when the user clicks lnk_Recipients in the click event.  So when they click the 'Edit' button I need to know what text is in column 3 for that selected row? I can't seem to find an example for something that should be so simple.  Can...

using button(click event) in panel with PopupControlExtender in gridview template column
   I want to use PopupControlExtender in gridview. when I click "link" linkbutton panel is visible.I want to insert the data into database  when I  click "Button1" then , however   the sql query is correct but the button1  dosen't insert. How can I trigger to button1.click? I am sorry for my english :( thanks...   <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptMan...

using button (click event) in panel with PopupControlExtender in gridview template column
 I want to use PopupControlExtender in gridview. when I click "link" linkbutton panel is visible.I want to insert the data into database  when I  click "Button1" then , however   the sql query is correct but the button1  dosen't insert. How can I trigger to button1.click? I am sorry for my english :( thanks...   <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID...

sending value in a column by clicking link button in gridview to another page
hi good morning everybodyplease bare with me as i am novice in the DOT.NET FRAMEWORK 3.5 VB.NET I have several table for registration of marrage profilesthere are more than 50 fields now I am designing administrators page who will tick the profile as paid /free registrationor as approved photograph etc for binding gridview I am picking some head coloumns check box | profile no | name| age| city | LinkButton now I want the link button click on the on the particular row should pick the profile no on that row and send it to see detailed profile to  SeeProfile.aspx page to di...

Changing the value of a "Hidden" column in a gridview VB.Net
Okay, now that I am able to get the values, I am havong an issue because part of my validation rule states that if a hidden column value comes back as null (which is okay) it goes to a functio to find the value that it should be populated with (even though it is still "invisible") this value must be passed back to the RPC (COBOL) program in order to run the update function.  So I have this bit of code to find the control (this works): If gdv_rcpt_srch.DataKeys(gdv_rcpt_srch.SelectedIndex).Values("CARR_MC_BUS_ID") <> "" Then ls_carr_mc_bus_...

Gridview column and control settings. Disable buttons, checkbox based on row value
I'm using C# Hello, I have a gridview with a checkbox in the first column along with a button in the last column. Based on a rows value (on load), I want to make the checkbox grey or not clickable and then enable the button in the last column for that row. If the value is different I want the checkbox clickable, but I want to hide or disable (make it grey) the button in the last column. I'm using the RowDataBound method and have access to the row as it loads. But do not know how to change the checkbox to grey or disabled (same for the button). Does anybody know how to disable the check...

Need to update column value in SQL database after clicking custom button in GridView
I am using ASP.NET 2.0 running on a Windows Server 2003 VM. I am using Visual Web Developer 2008 Express Edition (not relevant probably, but thought I'd mention it). I have created a GridView that utilizes a SqlDataSource. I added a Button field, converted it to a Template and added a Button to it. If I didn't convert it to a Template and add a Button from the toolbox I didn't have the ability to pass the CommandArguments (the button added to a GridView is different than a toolbox Button apparently). Also, I have the GridView set to color the rows based on a value in...

Bound columns and template columns in Gridview
Hello Everyone, In my gridview, I changed some of the bound columns to template columns, I noticed that the template column is not recognized inside the RowDataBound event method. The reason I changed the bound columns to template columns so that I can add the total column in the footer. I guess I cannot do that with bound columns. If I do e.Row.Cells[5].Text then  5th column is not recognixed inside the Rowdatabound event, but If i do e.Row.Cells[4].Text then  it is recognized. Please let me know what am I doing wrong. <asp:GridView ID="dgAgentActivity" runat="server" AutoGen...

How do you get a column value from a gridview column
How do you get a column value from a gridview column in asp.net 3.5? I tried this but it does not work.Dim myRow As GridViewRowFor Each myRow In GridView1.Rows    Dim strTest As String = DataBinder.Eval(myRow.DataItem, "EmailSubject")Nextif I change the column to a Template field then EmailSubject = myRow.FindControl("EmailSubject") works but I was hoping for a more direct way of getting a column value EmailSubject = myRow.Cells(4).Text also works but is not flexible if I change the column orderThanks Craig Try: Dim strTest As String = CType(...

Solution Title: Datagrid Template Column Radio Button (get access to the rb control)
Hi. I have a radio button in a template column of a datagrid. All I need to know is how to get access to this to be able to set the radio button as checked or unchecked (depending on the results of a query). I'm trying this: Dim rbactionneeded As RadioButton = CType(e.Item.FindControl("rbactionneeded"), RadioButton) If ansval = "Y" Then 'result from stored procedure rbactionneeded.Checked = True End if (very simplified). I'm getting an erro...

Hide Gridview column based on the column value
I have a grid which i bind with values from Database. I have events to edit and update the grid. I am not using SqlDatasource to connect to DB. Rather i am doing the updating of the grid Programmatically. The Grid is populated based on a dropdown selection Gridview has 4 columns, LID(item template column) ,LVal (Item Template and an Edit Item template column), Lookupid (Item Template and an Edit Item template column) and "Edit" link 4 of 10 Dropdown selections have values in Column 3 (lookupid), rest dont. When they dont have values i want to hide that column 3. Of course i can make this column hide based on Dropdown value. But I want to do it in the grid level.thecolumn 3 coming in is of type Decimal. I tried to do the following in rowdatabound event Question? How do i hide column 3 ingridview if there are no values coming in. I tried following gvLv_RowDataBound Event If e.Row.Cells(2).Text = "" Then   gvListvalues.HeaderRow.Cells(2).Visible = FalseEnd If I got Index out of range exception from the catch block where i am binding the gridview.Control come to first line goes back to catch block where i am binding the grid and throws this exception. And tried following code after Grid DataBind() methodFor Each gvlist As GridViewRow In gvLv.Rows()                    If gvLv.Rows(0).Cells(2).Text = "" Then     ...

How to get GridView column values while looping through the columns?
Hi everybody, I'm trying to figure out how can I get values from DataFields while  looping through the Grid rows. Here is what I'm trying to do:GridViewRow row = null; for (int i = 0; i < this.ProfilesGrid.Rows.Count; i++ ) {row = this.ProfilesGrid.Rows[i];   CheckBox chkbtnApproved = row.FindControl("chkApproved") as CheckBox ;if (chkbtnApproved != null) {if (chkbtnApproved.Checked == true) {int RegID = (int)this.ProfilesGrid.DataKeys[i].Value ;   //PersonName = row.DataItem["FirstName"] + " " + row.DataItem["LastNam...

Web resources about - How to access GridView hidden Column value by clicking a template column button? - 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: 3/13/2016 12:23:14 AM