Link to home
Start Free TrialLog in
Avatar of PAR6
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?
Avatar of Solar_Flare
Solar_Flare

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
Avatar of PAR6

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.
ASKER CERTIFIED SOLUTION
Avatar of Solar_Flare
Solar_Flare

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PAR6

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("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,.....)

        }
    }
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
Avatar of PAR6

ASKER

"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.
Avatar of PAR6

ASKER

So basically, I can't get it to write the correct string from Loop values for insert method. (var1.Text,var2.Text,...);
Avatar of PAR6

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.