|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Object reference not set to an instance of an object
I understand the problem here, just not the solution.
I am creating a webpage using ASP that dynamically creates input textboxes, and then takes any text from those boxes and writes them to a database. The Textboxes are named dynamically depending on how many there are. For example, if 26 text boxes are needed, they are created and named from "Text0" to "Text25". Unfortunately because these are created during runtime, I am unable to call them to extract the information that might be in them. For example, if I wanted to call: text1.text.toString() and save it to a value, it'll give me the "Object reference not set to an instance of an object" error. I've tried sneaky ways of trying to get around it, but in every case (even if the compiler doesn't yell at me for syntax), as soon as I call the runtime variable, it'll give me that error. Does anyone know how I can call runtime variables in VB.NET? Thanks |
|
#2
|
|||
|
|||
|
Assuming you create your textboxes this way:
Code:
<% for (int i = 0; i < 3; i++) { %>
<input name="text<%=i%>" type="text">
<% } %>
...you can access its text values via... Code:
private void btnSubmit_Click(object sender, EventArgs e) {
foreach (string key in Request.Form) {
if (key.StartsWith("text")) {
Response.Write(key + ": " + Request.Form[key] + "<br />");
}
}
}
Anyhow...you can always access each element inside your form via Request.Form[string key]. If you have dynamic stuff imho this is the only way. Wingman |
|
#3
|
||||
|
||||
|
This is my main issue with VB... C# will not even let you compile that because there is no variable called "text1" (for example) defined in the current scope - (if you create a server-side textbox at runtime there is still no member variable associated with it)
One thing you could do is give each of your newly created controls an ID and then use FindControl (member of Page) - the method returns a Control, so you will have to cast the return to a TextBox (or whatever) to use properties pertaining to that specific object - although being VB it will quite happily perform the conversion for you - you just won't have any decent intellisense for it. Code:
VB
Dim text1 as new Textbox()
text1.ID = "text1"
Controls.Add(text1)
...
FindControl("text1").Text = "text"
Code:
C#
Textbox text1 = new Textbox();
text1.ID = "text1";
Controls.Add(text1);
...
((Textbox)FindControl("text1")).Text = "text";
__________________
Scott Perham - MCPD Ekina.net - Application Design That URL too long? Why not URL IT! |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Object reference not set to an instance of an object |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|