Dynamically assign textbox ID to a textbox variable in a loop?

G

Guest

I try to use "for" loop to assign textbox control ID to a textbox variable in
server side codebehind for a web form. But I met some problem.
What I want to do is solving the following-like code by a loop:
static code:
txtQ1.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q1")
txtQ2.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q2")
.....
txtQ10.Text = ds.Tables("mmsSpecRecord").Rows(0)("Q10")

where Q1, .. Q10 are field name of a table; and txtQ1, ..., txtQ10 are
textbox control ID in design.

I implement them again in a FOR loop as follows
------code -------
Dim field As String = "Q"
Dim score As TextBox
For i As Integer = 1 To 35
field = "Q" + CStr(i)
score.ID = "txtQ" + CStr(i)
If Not IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field)) Then
score.Text = ds.Tables("mmsSpecRecord").Rows(0)(field)
Else
score.Text = "NA"
End If

Next
--------------


I got error message:
----------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information about
the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:


Line 228: For i As Integer = 1 To 35
Line 229: field = "Q" + CStr(i)
Line 230: score.ID = "txtQ" + CStr(i)
Line 231: If Not
IsDBNull(ds.Tables("mmsSpecRecord").Rows(0)(field)) Then
Line 232: score.Text =
ds.Tables("mmsSpecRecord").Rows(0)(field)


Source File: c:\inetpub\wwwroot\Demo\displayMMS.aspx.vb Line: 230
 
S

Swanand Mokashi

Have you tried using the FindControl method ?

Dim score As TextBox
Dim id as String = "txtQ" + CStr(i)

score = (TextBox) Page.FindControl(id)

and then score.Text = .... etc


--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
 
Z

zombie

Hi,

You are getting the error message because you have not initialized the
TextBox object. Put the line score = new TextBox at the start of the
For loop, it will work. Also if you are initialiazing an object in the
loop it would be good if you put score = nothing at the end of the loop
if you are not going to use this object again.

Regards,
Peeyush.
 
G

Guest

Hi, zombie:
I have tried it as your comment, there is no error. But the score as i=10 is
not txtQ10. So the txtQ10.Text = "" and score.Text = 0 as i=10 (from
database).

David
 
G

Guest

Thank you, Swanand.
Combined your comment and zombie's, it works. The type conversion would be
score = CType(FindControl("txtQ" + CStr(i)), TextBox)

David
 
G

Guest

Thank you, zombie.
Combined your comment and Swanand's, it works. The type conversion would be
score = CType(FindControl("txtQ" + CStr(i)), TextBox)

David
 
S

Swanand Mokashi

makes sense -- I think in C# :)
david said:
Thank you, Swanand.
Combined your comment and zombie's, it works. The type conversion would be
score = CType(FindControl("txtQ" + CStr(i)), TextBox)

David
 

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

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,832
Latest member
UtaHetrick

Latest Threads

Top