get edit template control value in Gridview

 I have a gridview that has two edit item templates which have controls textbox and a label I want to get the value of the controls when I update the gridview and add that to the update parameters. I keep getting object reference not set to an instance of an object for the label and the textbox. I cannot figure this out.
I can't believe this has stumped me as I know I have done it before and it is very simple I must be over looking something.
here is my vb

here is my vb 

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs)
Handles GridView1.RowUpdating

Dim varSSN As Integer = CType(GridView1.FindControl("lblSSNed"), TextBox).Text
Dim varEmpID As Integer = CType(GridView1.FindControl("txtEmpID"), TextBox).Text

sdsMissedImports.UpdateParameters.Item("SSN").DefaultValue = varSSN
sdsMissedImports.UpdateParameters.Item("EmpID").DefaultValue = varEmpID

End Sub

here is my gridview
<asp:GridView ID="GridView1" runat="server"          <Columns>
"True" />

<asp:TemplateField >
<p> Enter This employees Employee ID and Click update to
<br /> import all of this employees missed imports into Health/Dental Etc Databases.
Please Verify you have the correct EMPID before doing this</p>
<asp:Textbox id="txtEmpID" runat="Server" />
<asp:RequiredFieldValidator ID="reQEmpID" runat="server" ControlToValidate="txtEmpID" ErrorMessage="EmpID is Required" />
<asp:RegularExpressionValidator runat="server" ID="RegexEmpID" ErrorMessage="Please Enter EMPID no Letters & Max of 11 numbers" ControlToValidate="txtEmpID" Font-Bold="True"
ValidationExpression="(\d{1,11})" Display="None"></asp:RegularExpressionValidator>
<ajaxToolkit:ValidatorCalloutExtender ID="EmpCallOut" runat="server"
TargetControlID="RegexEmpID" Enabled="True"></ajaxToolkit:ValidatorCalloutExtender>
<asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="True" SortExpression="Name" />
<asp:TemplateField HeaderText="SSN" SortExpression="SSN">
<asp:Label ID="lblSSNed" runat="server" Text='<%# Eval("SSN") %>'></asp:Label>
<asp:Label ID="lblSSN" runat="server" Text='<%# Bind("SSN") %>'></asp:Label>
4/15/2009 3:19:55 PM 93655 articles. 5 followers. Follow

2 Replies

Similar Articles

[PageSpeed] 6

Dim varSSN As Integer = CType(GridView1.FindControl("lblSSNed"), TextBox).Text 'GridView1.SelectedRow.Cells(1).Text
Dim varEmpID As Integer = CType(GridView1.FindControl("txtEmpID"), TextBox).Text


Please note that FindControl is not recursive. You need to specify the rows and cells when referencing a control that resides within GridView template. Here's an example:


'Just changed the index of cells based on your requirements
Dim tb As TextBox = DirectCast(GridView1.Rows(e.RowIndex).Cells(0).FindControl("txtEmpID"), TextBox)
Dim lb As Label = DirectCast(GridView1.Rows(e.RowIndex).Cells(1).FindControl("lblSSNed"), Label)
Dim id As String = tb.Text
Dim ssNed As String = lb.Text

'do something with the values here


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

How to get your Forum Question Answered | Blog | CodeASP.NET
4/15/2009 3:36:05 PM

That did the trick Thanks! Overthinking I guess

4/16/2009 2:59:14 AM

Similar Artilces:

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

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

How to get the HREF value of the Anchor tag in a web page using the web browser control in
Hi Friends,               i have a webserver control in my application.i load the web browser control on a button click using the below code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click                AxWebBrowser1.Navigate("", False) End sub once the page is loaded in the browser i want the <a> HREF value in that page.For example if that page has ...

Trying to edit web form, can't get database updated with newly edited values.
 This is my code:  when I try to update any of the fields, the update statement retains the old values retrieved from the database.  Could you tell me where my code is bad? -Thanks, Mark   <%@ Page Language="VB" %><%@Import Namespace="System.Data"%><%@Import Namespace="System.Data.SqlClient"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""><script runat="server"> Protected Sub Page_Load(ByVa...

Get values from gridview edit template
I have a gridview that I update without a datasource.  Problem is, when I initiate the update of the row's...  the code behind is not getting the information in gridview textboxes.  Any suggestions??? <Columns> <asp:BoundField DataField="COUNT_REQUEST_ID" HeaderText="ID" ReadOnly="true" /> <asp:TemplateField HeaderText="X Coordinate"> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Eval ( "X_COORDINATE" ) %>' /> </ItemTemplate> <E...

Select Value of Gridview Template Control During Edit
Im trying to access the value of a textbox within a template field during edit mode so that I can update that record. I had it working before I converted the columns to template fields, but I changed it so I could control the width of the gridview once if was in editmode. (Wasnt sure if there was any other way to stop it expanding.) I have this code;  myValue = DirectCast(Gridview1.Rows(0).Cells(1).FindControl("Textbox1"), TextBox).Text  This only works on the first record of my gridview. Im assuming because of the "Rows(0)". Any other record I get; Object...

Web User Control in GridView Edit Template
Hi All, I have created a Web User Control (lets call it ACP for short), that combines an autocomplete extenter with some images and some javascript into one control.  The ACP control works great when located on a page or inside another Web User Control. What I want to do now is place the ACP into a gridview's EditItemTemplate.<EditItemTemplate>    <uc1:AutoCompletePlus ID="acpAthleteNameGVEdit" runat="server" ServicePath="../webservices/AutoComplete.asmx" ServiceMethod="GetAthleteNameCompletionList" ConfineUserTo...

How To Convert web template to Forms Template
I need  Convert web template to Forms Template   There are some free c# to vb converters out there which will allow you to upload a project and convert it to vb.   The programming models for windows forms and web projects are very different there is no way to convert a web to windows forms project.      Silverlight-helpVb TipsSpace Coast .Net User Group Hey Ajax-y, This tool really helps all programmers. I thought I would share this with you in hopes that you will fine some use for it. Thanks to KamalPatel.Net for this tool. You can see the tool here.  Hope this helps!!    Please give us feedback no matter whether you get your answer.Please "mark as answer" if it's useful for youSincerely,SpongeBert...

How do I edit the web form template in VS.NET 2008?
Hi, I'd like to edit the template VS.NET 2008 uses for creating web forms for me as I'd like to remove all the extra spaces and things. I remember I could do this w/ VS.NET 2003. Since then I've forgotten where to locate the templates. I'd appreciate some pointers here.Thanks,Sam You would probably be better off creating your own new template... but this can be based on a copy of the existing one.Creating project and item templates is covered on MSDN: Richard...

GridView and Javascript
Hey All, I need help from  you. I have a GridView. In the  Edit Item template of the first Column I have a textbox, an user control and hidden field. When I click on the user control it  opens a pop up from which I can choose the required value. The text value is populated in the textbox and Id in the hidden field. Here's the code for edit template.         <EditItemTemplate>                        <table cellpadding ="0" cellspacing ="0">                            <tr>                                <td>                                    <asp:TextBox id = "txtEditKnownBy" runat ="server" ReadOnly ="true"  Text='<%# DataBinder.Eval(Container.DataItem,"userName")%>'></asp:TextBox>                     ...

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 Hiuse this: VB:  Protected Sub GridView1_RowCommand(ByVal ...

Clearing Web Controls Values from a Web Form
I have a web form and after a user submit it I want to clear the form so that they may submit another entry. I have seen examples in VB of how to loop thru a web form and clear the controls, but I need a C# solution. I have tried the following code, while it does not give me an error, it does not clear the controls on the form. Any suggestions? Thanks! foreach (Control parent in Page.Controls)         {             foreach (Control child in parent.Controls)           ...

getting values from a web form to windows form
Hot Requirement ..... Is there any possible of getting values from a web form to windows form ????? Yes One way i to use the WebRequest and WebResponse classes of the System.Net namespace. For example:  WebRequest request = WebRequest.Create("URL of the web page); WebResponse response = request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string content = reader.ReadLine(); ....    Reza Nassabehwww.professionalcsharp.comDon't forget to click "Mark as Answer" on the post that helped you. That way fut...

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

Retreiving a value from a web form within a web user control
Hi  I have a page (web form) that retreives some data from a database. To be more specific: at some point when user clicks a button (in the web form) there is some ID number retreived from a database in code behind. At the same time the Visible property of the nestet web user control is chamged to true. I want than forward the ID to the web user control so that a grid view can load data from a database for that ID. How can I access that data (the ID) within a web user control context nested in that page?  Kind regards   Check this ;ink

Add new controls to a gridview row; when editing that row using the edit row feature in the gridview control.
Hi all  I have a questions that looks hard / complex to me, and i know you can help me in this. Q {  I have row in a Gridview and i have an edit option to it. I press the edit button, to edit the values in that row and update data all the way back to the database. Now when i press the edit button, i want a drop down list to be displayed that contain values for a particular field in my grid view. (for now only plain text boxes appear.) } I hope you understand my question, all i need to do is replace the text box controls with a dropdown list when i am editing a row in ...

Getting/Updating and Binding a Control in the Edit Template of a Grid View Control
 Hi,               I am curious how do I update (or data bind) a drop down list in the edit template of a Grid View Control.             My problem is that in the GridView_RowEditing() method, the drop down list does not seem to exist. It only appears when the DropDownList_DataBound() method gets called. I can get the data for the DropDownList and attempt to bind it in this method, but then calling the DropDownList.DataBind()calls DropDownList_DataBound(), a...

Controlling web control instances on Web Form
I have created a composite control, extending functionality of the Hidden Control. However, I would like to prevent the user from adding this control to the page multiple times. I only want one instance of the control on the Web Form. What setting in my project to I adjust to enable this functionality? I can not find it in any of the books that I have referenced. Is this a setting or is this code that has to be added to the inheriting class. Looks like there is no standard apporach. But I'd consider using static field to count number of created objects and throw exception if second object ...

Getting the Selected Item of dropdownlist inside Web User Control on a Web Form
Ive got a dropdownlist control inside a web user control called Entity.ascx. This web user control is on a webform.aspx. In Entity.ascx class I created a SelectedEntity property that returns the selected item in the dropdownlist. Inside webform.aspx when btnGo is pushed I set a string and pass it in the url. I am trying to get the entity they selected via the property (SelectedEntity) inside Entity.ascx. As the SelectedEntity property tries to return the selected item in the dropdownlist I get this error: System.NullReferenceException: Object reference not set to an instance...

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" AutoGenerateColumns="False" BackColor="Navy" BorderColor="IndianRed" BorderStyle="Solid" Font-Names="Verdana" Font-Size="X-Small" DataSourceID="SqlDataSource_grid" AllowPaging="True" AllowSorting="True" ForeColor="White" DataKeyNames="legid"> <Columns> <asp:CommandField ShowEditButton="True" CancelImageUrl="~/App_Graphics/quit.gif" CancelText="" EditImageUrl="~/App_Graphics/EditGrid.GIF" EditText="" UpdateImageUrl="~/App_Graphics/save.gif" UpdateText="" ButtonType="Image" /> <asp:BoundField DataField="StartDate" HeaderText="Start Date" ReadOnly="True" /> <asp:BoundField DataField="EndDate" HeaderText="End Date" ReadOnly="True" /> <asp:BoundField DataField="CounterpartDealRef" HeaderText="CP Deal Ref" ReadOnly="True" />   <asp:TemplateField HeaderText="Preliminary Settlement Price" ><ItemTemplate> <asp:Label ID=lblPreliminary runat=server ...

Can't find a GridView inside a template of a custom templated web control
This is my first Templated Web Control I've written and I hope someone can help. I have three templates inside my web control and I'm trying to find a GridView inside one of them.  Naturally, it's throwing a null reference exception.  Here is the code for my user control:  1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Text; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 9 namespace MyWebControls 10 { 11 [ToolboxData("&lt;{0}:Sec...

Passing Values from a Web Form to Another Web Form
Hello All! I am trying to pass values from one web form to another web form. I know that there are 3 ways of doing this: queryString, Server.Transfer and Session. I am using the Server.Transfer method. But I am not able to create, in my 2nd web form, an instance of my 1st web form. My 1st Web form is named CallTrack.aspx and my 2nd is named Export.aspx. What I mean is something like this: Public value as CallTrack. I can't create it in my 2nd web form because when the intellisense is displayed, there's no CallTrack in the options. Thanks!   How about using cross-...

how to call controls of web control form in coding of other forms?
hello everyone i have one problem i have created one webcontrol shoppingcart in which there are two labels one of qty and other of amt which are initially enable =falsenow on other page(aspx page) i have one txtbox of qty as txtqty i want tat as soon as user enters qty txtqty and clicks on add cart button it shd be display d value in lblqty of web control shopping cart make sure i m using datalist on aspx page so give me suggestions on tat basis and my coding is in vb. plz give ur suggestions as fast as possible. Neeti what do you mean by webcontrol.... it this user control or normal contols.... on the other page(aspx page)... means are you using the user control in aspx page...Chinna_sv... Hi, If your Label Control resides withing a UserControl or in a Data Representaion Controls like (GridView, FormView, DataList etc..) then you can use FindControl method to reference/access the controls in your codes..Regards,Vinz"Code, Beer and Music" that's my way of being a programmer!How to get your Forum Question Answered | Blog | CodeASP.NET...