PAR6
asked on
Dynamic Textbox ID property from database in repeater asp.net 2.0
Hello Experts,
I am creating a dynamic form of textboxes generated off of sql table.
The textbox properties can be set dynamically EXCEPT the "ID".
error message:
The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />
I've tried:
<asp:TextBox ID=<%# Eval("field_id") %>...
Or
<asp:TextBox ID=<%# Bind("field_id") %>...
What SHOULD I be doing?
How else can I retrieve user input from dynamically created controls?
I am creating a dynamic form of textboxes generated off of sql table.
The textbox properties can be set dynamically EXCEPT the "ID".
error message:
The ID property of a control can only be set using the ID attribute in the tag and a simple value. Example: <asp:Button runat="server" id="Button1" />
I've tried:
<asp:TextBox ID=<%# Eval("field_id") %>...
Or
<asp:TextBox ID=<%# Bind("field_id") %>...
What SHOULD I be doing?
How else can I retrieve user input from dynamically created controls?
ASKER
Thanks Solar Flare
My table has the columns: field_id, label, default_text, mode (0,1,2), visible(true/false)...
The admin would fill out a form that stores the properties of text boxes in a db table... which a user will then later fill-out the dynamically generated form.
I'm using a repeater to dynamically create textboxes.
One problem. It won't dynamically create IDs thus I can't input form values into another DB table. Not sure of when "findcontrol" will work.
Can you please specify a bit more on the databinding and where "findcontrol" will come in play?
Thank you.
My table has the columns: field_id, label, default_text, mode (0,1,2), visible(true/false)...
The admin would fill out a form that stores the properties of text boxes in a db table... which a user will then later fill-out the dynamically generated form.
I'm using a repeater to dynamically create textboxes.
One problem. It won't dynamically create IDs thus I can't input form values into another DB table. Not sure of when "findcontrol" will work.
Can you please specify a bit more on the databinding and where "findcontrol" will come in play?
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for the quick response.
I've tried it in C# and response.write to convince myself that the loop actually works. Can you help me cross the finish line?
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem r in DataList1.Items)
{
TextBox t = ((TextBox)(r.FindControl(" Text1")));
Label l = ((Label)(r.FindControl("La bel1")));
Response.Write(t.Text);
/// I want to do something here to get values into database using class+storedProcedure i.e. someClass.insertUserForm (t1.Text,t2.Text,.....)
}
}
I've tried it in C# and response.write to convince myself that the loop actually works. Can you help me cross the finish line?
protected void Button1_Click(object sender, EventArgs e)
{
foreach (DataListItem r in DataList1.Items)
{
TextBox t = ((TextBox)(r.FindControl("
Label l = ((Label)(r.FindControl("La
Response.Write(t.Text);
/// I want to do something here to get values into database using class+storedProcedure i.e. someClass.insertUserForm (t1.Text,t2.Text,.....)
}
}
i use vb so the example would be in that...
you would add a new class, say DBUtil
class DBUtil
public shared Function InsertUserForm(var1, var2, var3) as boolean
dim dbconn as sqlconnection
try
dbconnection.COnnectionstr ing = "your connection string"
dbconn.open()
dim cimm as sqlcommand = new sqlcommand
comm.Commandtype = commandtype.StoredProcedur e
comm.Connection = dbconn
comm.CommandText = "..."
'any other initialisation/setup/varia bles for the sqlcommand
comm.executenonquery()
return true
catch
return false
finally
if dbconn.State = connectionstate.open then dbconn.close()
end try
end class
then you can call DBUtil.InsertUserForm(var1 , var2, ...) from your code
you would add a new class, say DBUtil
class DBUtil
public shared Function InsertUserForm(var1, var2, var3) as boolean
dim dbconn as sqlconnection
try
dbconnection.COnnectionstr
dbconn.open()
dim cimm as sqlcommand = new sqlcommand
comm.Commandtype = commandtype.StoredProcedur
comm.Connection = dbconn
comm.CommandText = "..."
'any other initialisation/setup/varia
comm.executenonquery()
return true
catch
return false
finally
if dbconn.State = connectionstate.open then dbconn.close()
end try
end class
then you can call DBUtil.InsertUserForm(var1
ASKER
"DBUtil.InsertUserForm(var 1, var2, ...) from your code"
Unfortunately, that's the part I'm having difficulties. Since the textboxes will be created dynamically, how do I get the var1 var2 var3 etc from your previous post?
-------------------------- ---------- ---------- ---------- ----------
for each r as repeateritem in repeater.Items
dim t as textbox = CType(r.FindControl("Text1 "), Textbox)
dim l as label = CType(r.FindControl("Text1 "), label)
'if the textbox and label are inside another control you may need to do a findcontrol for parentcontol first, then do parentcontrol.findcontrol to get your actual textbox
'then do something using the value t.Text which is what the user entered
next
-------------------------- ---------- ---------- ---------- ---------- --
Thank you for your responses so far. I see the finish line... it's close.
Unfortunately, that's the part I'm having difficulties. Since the textboxes will be created dynamically, how do I get the var1 var2 var3 etc from your previous post?
--------------------------
for each r as repeateritem in repeater.Items
dim t as textbox = CType(r.FindControl("Text1
dim l as label = CType(r.FindControl("Text1
'if the textbox and label are inside another control you may need to do a findcontrol for parentcontol first, then do parentcontrol.findcontrol to get your actual textbox
'then do something using the value t.Text which is what the user entered
next
--------------------------
Thank you for your responses so far. I see the finish line... it's close.
ASKER
So basically, I can't get it to write the correct string from Loop values for insert method. (var1.Text,var2.Text,...);
ASKER
For the benefit of others with similar issue-
Here are the techniques used:
StringBuilder
foreach loop nested in do while loop
manipulate StringBuilder .append to create final insert method string.
Here are the techniques used:
StringBuilder
foreach loop nested in do while loop
manipulate StringBuilder .append to create final insert method string.
put an asp:placeholder in your page then in your code:
dim x as textbox = new textbox
x.ID = ...
x.text = ...
placeholder.controls.add(x
then use findcontrol if you know the names, else do
for each c as control in placeholder
if typeof(c) is Textbox then
...
end if
next
hope that helps