473,395 Members | 1,623 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem in reading Values from dybamically added controls

Hi All,

I am not able to read the values from Gridview TemplateField Controls.

basically I craeted a gridview with four colums and finally i added a Template field to it.

The templatefield contains 3 labels and one button while i am clicking on this button. i am getting a error message


Error "Object reference not set to an instance of an object" - .NET VB

Please suggest what should i do for thi problem

this is my Buttons.aspx Page

<%@ Page Language="VB" Debug="true" AutoEventWireup="false" CodeFile="button.aspx.vb" Inherits="button" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Buttons in Gridview</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<div>
&nbsp;<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="gid"
DataSourceID="SqlDataSource1" OnSelectedIndexChanged="GridView1_SelectedIndexCha nged">
<Columns>
<asp:BoundField DataField="fn" HeaderText="First Name" SortExpression="fn" />
<asp:BoundField DataField="ln" HeaderText="Last Name" SortExpression="ln" />
<asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
<asp:BoundField DataField="regdate" HeaderText="Registered Date" SortExpression="regdate" />
<asp:TemplateField>
<ItemTemplate>
<asp:Label id="fn" runat="server" Text='<%# Eval("fn") %>'></asp:Label><br>
<asp:Label ID="ln" runat="server" Text='<%# Eval("ln") %>'>'></asp:Label><br>
<asp:Label ID="email" runat="server" Text='<%# Eval("email") %>'></asp:Label><br>
<asp:Label ID="rd" runat="server" Text='<%# Eval("regdate") %>'></asp:Label>
<asp:Button ID="Button1" runat="server" Text="Display" CommandName="disp"/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:My_Connection_String %>"
SelectCommand="SELECT [fn], [ln], [email], [regdate], [gid] FROM [GLogin]"></asp:SqlDataSource>
</div>
</form>
</body>
</html>

And This is my code behind file

Imports System.Data.SqlClient
Imports System.Data
Partial Class button
Inherits System.Web.UI.Page

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs ) Handles GridView1.RowCommand
' Dim dr As DataRow
Dim index As Integer
index = Val(e.CommandArgument)
Dim sr As GridViewRow = GridView1.SelectedRow
If e.CommandName = "disp" Then
Dim l As String
MsgBox(sr.Cells(5).Controls(4).ID)
l = CType(sr.FindControl("fn"), Label).Text
MsgBox(e.CommandSource.ToString)
End If
End Sub

Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged

End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub
End Class

I need a quick solution for this
May 1 '07 #1
0 1184

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Punisher | last post by:
I'm adding checkbox controls to a table dynamically when the form loads for the first time. When a postback occurs, the controls are not maintained in the table. How do I get them to retain...
9
by: Anders K. Jacobsen [DK] | last post by:
Hi I have this that adds some usercontrol (UCTodays.ascx) to a placeholder foreach(A a in B){ UCTodays ucline = (UCTodays )LoadControl("UCTodays.ascx");...
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
1
by: Guadala Harry | last post by:
I will be creating a new ASPX that requires many controls to be inserted into the page at runtime. The controls include things like TextBox, RadioButtonList, CheckBoxList, DropDownList, etc....
4
by: Larry Grady | last post by:
Anyone up for a challenge? I've been struggling with this for a few days and was hoping someone could help me. Pouring through all the messageboards I just can't find the solution. We have a...
1
by: KasperBirch | last post by:
Hi NG Im trying to read the values of a bunch of TextBox'es that I add in a loop, dynamically. The page has an <asp:Table id="table" ...> control, and I add rows with textboxes like this (from...
0
by: anandv81 | last post by:
Hi, I encountered a strange problem while working on an application, the problem goes like this. I am generating a few textboxes at runtime at the server side and added to a placeholder, a...
1
by: Kevin R | last post by:
This is one of the weirdest problems I have ever run into. I have had to trim down a bunch of code to give a sample that is more easily readable by those who will view this. Here is the problem:...
0
by: Mike Collins | last post by:
I someone can please help, I am about at an end in trying to figure this out. I am adding some dynamic controls to my page (I found out that I was supposed to be doing that in the oninit event,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.