New row in table problem

liunx

Guest
I have a new row appear in a table when a certain selection is made from a dropdown list.

This new row contains a textbox. When the submit button is pressed I would like to save the text in this textbox to the database. The problem is that it saves blank.

I'm sure it's not the code that's wrong because it works fine when I save text from any other textbox just not the one that is created dynamically.

Any ideas are much appreciated!The trick to dynamic controls is that during each postback you have to recreate the controls in order to work with them. Like this:

Private Sub Page_Load(byval sender as Object, byval e as EventArgs) Handles MyBase.Load
DynamicControls_Load()
End Sub

Private Sub DynamicControls_Load()
Dim txtBox as New System.Web.UI.WebControls.Textbox
txtBox.ID = "txtBox"
txtBox.ClientID = "txtBox"
Page.Controls.Add(txtBox)
End Sub

Private Sub btnSubmit_Click(Byval sender As Object, Byval e As EventArgs) Handles btnSubmit.Click
Dim strText As String = Ctype(Page.FindControl("txtBox"),System.Web.UI.WebControls.Textbox).Text
End Sub


The textbox is recreated during each postback and this allows it to be accessed. I've read that the order in which they are recreated is also important, but I haven't run into that problem yet. My example is very simple and yours will likely be more complex, however this should shed some light on your problem. Hope that helps.Hi, thanks loads for replying - nobody else seems to have any idea why this wont work!!!

I'm getting this error now:
"Object reference not set to an instance of an object."
Dim strText As String = CType(Page.FindControl("txtBox"), System.Web.UI.WebControls.TextBox).Text

It still can't seem to find my textbox aghhhh!!!!!!!!1Ok, here is a completely working example from the code that I gave you. On page load it adds the textbox to the form. When you type text into the textbox and click the button, then the button event will response.write the value of the textbox. This of course is a very basic example.

<%@ Page Language="VB" %>
<html>
<head>
<script runat="server">
Private Sub Page_Load(byval sender as Object, byval e as EventArgs)
DynamicControls_Load()
End Sub

Private Sub DynamicControls_Load()
Dim txtBox as New System.Web.UI.WebControls.Textbox
txtBox.ID = "txtBox"
Form.Controls.Add(txtBox)
End Sub

Private Sub btnSubmit_Click(Byval sender As Object, Byval e As EventArgs)
response.write(Ctype(Page.FindControl("txtBox"),System.Web.UI.WebControls.Textbox).Text)
End Sub
</script>
</head>
<body>
<form id="Form" runat="server">
<asp:button id="btnSubmit" onclick="btnSubmit_Click" text="Submit" runat="server" />
</form>
</body>
</html>Here's another example which adds a literal control as well as a button with it's click event wired up.

<%@ Page Language="VB" %>
<html>
<head>
<script runat="server">
Private Sub Page_Load(byval sender as Object, byval e as EventArgs)
DynamicControls_Load()
End Sub

Private Sub DynamicControls_Load()
Dim txtBox as New System.Web.UI.WebControls.Textbox
txtBox.ID = "txtBox"
Form.Controls.Add(txtBox)

Form.Controls.Add(New LiteralControl("<br />"))

Dim btnSubmit As New System.Web.Ui.WebControls.Button
btnSubmit.ID = "btnSubmit"
btnSubmit.Text = "Submit"
AddHandler btnSubmit.Click, AddressOf btnSubmit_Click
Form.Controls.Add(btnSubmit)
End Sub

Private Sub btnSubmit_Click(Byval sender As Object, Byval e As EventArgs)
response.write(Ctype(Page.FindControl("txtBox"),System.Web.UI.WebControls.Textbox).Text)
End Sub
</script>
</head>
<body>
<form id="Form" runat="server">

</form>
</body>
</html>Sorry to be a pain but I get:
Object reference not set to an instance of an object.
For the line:
Dim strText As String = CType(page.FindControl("txtBox"), System.Web.UI.WebControls.TextBox).Text

I really don't know why it can't find my textbox, I did the same as you!Here's where I add the control:
td2 = New HtmlTableCell
td2.Controls.Add(txtBox)
txtBox.ID = "txtBox"

Please help!!!Oh, you need to set the ID attribute before adding it to td2's controls.
So it would be:
td2 = New HtmlTableCell
txtBox.ID = "txtBox"
td2.Controls.Add(txtBox)I get the same error!Are you adding the textbox during each postback? If you don't then it will not be found and you could get that error.

The error you are getting is vague and can be caused by an infinite number of reasons, could you post most or all of your code, I could get a better idea from that.The textbox is not created during postback, it is created during the selectedIndexChanged event of another conrtol.

I'll send you some of my code:

Submit:
If CategoryDropDown.SelectedIndex = 1 Then
Row.Item("Category") = txtbox
Row.Item("SubCategory") = txtbox2
Row.Item("Question") = QuestionText.Text
'etc for all boxes

ds.Tables("faqlog").Rows.Add(Row)
Adapter.Update(ds, "faqlog")

CategoryDropDown_SelectedIndexChanged: If CategoryDropDown.SelectedIndex = 1 Then
AddRow()
...

AddRow()
Dim td1 As HtmlTableCell
Dim td2 As HtmlTableCell
Dim td3 As HtmlTableCell
Dim td4 As HtmlTableCell
Dim tr As HtmlTableRow
Dim txtBox As New System.Web.UI.WebControls.TextBox
Dim txtBox2 As New System.Web.UI.WebControls.TextBox
Dim lbl As Label = New Label

'Create a new row
tr = New HtmlTableRow

'Create Columns
'Column 1
td1 = New HtmlTableCell
td1.Controls.Add(lbl)
lbl.Text = "New Category:"

'Column 2
td2 = New HtmlTableCell
txtBox.ID = "txtBox"
td2.Controls.Add(txtBox)

txtBox.Width = Unit.Pixel(288)

'Column 3
td3 = New HtmlTableCell
Dim lbl2 As Label = New Label
lbl2.Text = "New Sub-Category:"
td3.Controls.Add(lbl2)

'Column 4
td4 = New HtmlTableCell
td4.Controls.Add(txtbox2)
txtbox2.Width = Unit.Pixel(288)

tr.Cells.Add(td1)
tr.Cells.Add(td2)
tr.Cells.Add(td3)
tr.Cells.Add(td4)
table1.Rows.Insert(1, tr)

It saves all fields exept the value of txtbox 1 and 2 (the ones that are created dynamically)

Also- Don't know if this will help but when the submit button is clicked - the new row disappears.

Thanks loadsOk, looking at your code, I think that you are going out of your way to do something simple. I was under the impression that you were trying to allow the addition of up to an infinite number of rows. But you are infact only adding 1 when a certain value is selected from a combo box.

In your case, I would simply add the row to your html and set the visibility to false from your codebehind. When the correct value is selected from the combo box, then make the row visible.

Here's a simplified example:

<html>
<head>
<script runat="server">
private sub Page_Load(byval sender as object, byval e as eventargs)
If Not page.ispostback then
trNewCategories.Visible = False
End if
End Sub

Private Sub CategoryDropDown_SelectedIndexChanged()
trNewCategories.Visible = True
End Sub

Private Sub btnSubmit_Click()
'Save text from text fields & set textboxes values = "" & hide row
'Save
txtbox1.Text = ""
txtBox2.text = ""
trNewCategories.Visible = False
End Sub
</script>

<head>
<body>
<form id="Form" runat="server">
<table>
<tr>
<td></td>
</tr>
<tr id="trNewCategories" runat="server">
<td>New Category:</td><td><asp:textbox id="txtBox1" runat="server" /></td><td>New Sub Category:</td><td><asp:textbox id="txtBox2" runat="server" /></td>
</tr>
</table>
</form>
</body>
</html>Hi thanks, does that mean that there would be a gap in my table until the new row is added? I'd like it added as the second row you see :)Oh my God I can't believe how simple it is I didn't realise it would all move up to fill the gap in! I've been trying to work this out for weeks! Well at least it's over now, I still can't believe I didn't even try it that way! Ok I feel like an idiot now....

Thank you so much you have no idea :)Lol, glad to hear that it is working well for you now, I know how frustrating it can be to spend days on something.
 
Back
Top