Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 8556
  • Last Modified:

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?
0
PAR6
Asked:
PAR6
  • 5
  • 3
1 Solution
 
Solar_FlareCommented:
you can create the control at runtime

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
0
 
PAR6Author Commented:
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.
0
 
Solar_FlareCommented:
if each repeater item has 1 Label, one textbox with default text, a mode (not sure what for?) and a visible property then your repeater template could simply contain

<asp:label id=label1 runat=server/>
<asp:TextBox ID=text1 runat=server text='<%# Container.DataItem("field_id") %>'/>

you could set the whole lot invisible by placing them in a panel or tablerow and setting its visibility


then to extract the values again on postback you would do

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



you dont need to worry about setting the IDs in the repeater as it gives them all unique id's anyway
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
PAR6Author Commented:
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("Label1")));
            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,.....)

        }
    }
0
 
Solar_FlareCommented:
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.COnnectionstring = "your connection string"
   dbconn.open()

dim cimm as sqlcommand = new sqlcommand
comm.Commandtype = commandtype.StoredProcedure
comm.Connection = dbconn
comm.CommandText = "..."
'any other initialisation/setup/variables 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
0
 
PAR6Author Commented:
"DBUtil.InsertUserForm(var1, 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.
0
 
PAR6Author Commented:
So basically, I can't get it to write the correct string from Loop values for insert method. (var1.Text,var2.Text,...);
0
 
PAR6Author Commented:
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.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now