• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

generating textbox controls?

hi,

i wanna know how to generate textbox control dynamic?


i have a drodownlist and if the user select 3 i should create 3 textbox controls and if the user select  7 then 7 textbox control...

and i have to save it in the db and reload when the user comes back next time

thanks
0
nisarkhan
Asked:
nisarkhan
1 Solution
 
hendridmCommented:
Quick and dirty - might give you some ideas:

<%@ Page Language="C#" Debug="true" %>

   <script runat="server">
      
    private void Page_Load(object sender, System.EventArgs e)
    {
        if(Page.IsPostBack) {
            form.Controls.Add(new LiteralControl("<br /><br />"));
            for(int i=0; i<Convert.ToInt16(num.SelectedItem.Text); i++) {
                TextBox tb = new TextBox();
                tb.ID = "textbox_"+i;
                tb.Text = i.ToString();
                tb.Width = 60;
                form.Controls.Add(new LiteralControl("Textbox #" + i.ToString() + ": "));
                form.Controls.Add(tb);
                form.Controls.Add(new LiteralControl("<br />"));
            }
        }
    }

    </script>

<html>
<body>

Choose Number:

<form id="form" runat="server">

  <asp:DropDownList id="num" runat="server" AutoPostBack="true">
    <asp:ListItem Text="1" />
    <asp:ListItem Text="3" />
    <asp:ListItem Text="5" />
    <asp:ListItem Text="7" />
  </asp:DropDownList>      

</form>

</body>
</html>
0
 
code1994Commented:
do u have in vb.net pls ?
0
 
nisarkhanAuthor Commented:
im vb.net guy too, would you please paste me the code in vb.net but in the mean time i will try to convert it

one more thing: this code should work in .vb
based on the values im getting i will put this in
for and next loop or something...

thanks a lot
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
hendridmCommented:
I'm not a VB.net guy, so this may contain errors:

    Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)

        If Not Page.IsPostback Then
            form.Controls.Add(new LiteralControl("<br /><br />"))
            For i = 1 to Convert.ToInt16(num.SelectedItem.Text)
                Dim tb As TextBox = New TextBox
                tb.ID = "textbox_" & i
                tb.Text = i.ToString()
                tb.Width = 60
                form.Controls.Add(new LiteralControl("Textbox #" & i.ToString() & ": "))
                form.Controls.Add(tb)
                form.Controls.Add(new LiteralControl("<br />"))
            Next i
    End Sub
0
 
nisarkhanAuthor Commented:
im getting the error on

num
and form

my formname is frm so change it from form --> frm  //but still error  // name frm is not declared

and also im getting error on  num  // name num is not declared

thanks
0
 
nisarkhanAuthor Commented:
num is the dropdownlist box so i fixed


and instead of form im using me.

and still last error getting on

    tb.Width = 60  //im getting error on number 60 it  aster.aspx.vb(70): Value of type 'Integer' cannot be converted to 'System.Web.UI.WebControls.Unit'.
 
0
 
hendridmCommented:
<%@ Page Language="VB" Debug="true" %>

   <script runat="server">

    Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)

        If Page.IsPostback Then
            Dim i As Integer
            frm.Controls.Add(new LiteralControl("<br /><br />"))
            For i = 1 to Convert.ToInt16(num.SelectedItem.Text)
                Dim tb As TextBox = New TextBox
                tb.ID = "textbox_" & i
                tb.Text = i.ToString()
                tb.Width = Unit.Pixel(60)
                frm.Controls.Add(new LiteralControl("Textbox #" & i.ToString() & ": "))
                frm.Controls.Add(tb)
                frm.Controls.Add(new LiteralControl("<br />"))
            Next i
        End If

    End Sub

    </script>

<html>
<body>

Choose Number:

<form id="frm" runat="server">

  <asp:DropDownList id="num" runat="server" AutoPostBack="true">
    <asp:ListItem Text="1" />
    <asp:ListItem Text="3" />
    <asp:ListItem Text="5" />
    <asp:ListItem Text="7" />
  </asp:DropDownList>      

</form>

</body>
</html>
0
 
hendridmCommented:
Change tb.width = 60 to:
tb.Width = Unit.Pixel(60)
0
 
nisarkhanAuthor Commented:
after i run i get this error:

Control 'textbox_1' of type 'TextBox' must be placed inside a form tag with runat=server.

:)
0
 
hendridmCommented:
Does your form tag look like this?

<form id="frm" runat="server">
0
 
nisarkhanAuthor Commented:
i should put the code inside the aspx or .vb file ?

rite now i put my code inside the .vb file
0
 
hendridmCommented:
If you want to be able to use the above code exactly, use .aspx.  Otherwise, you can use codebehind to put it in a .vb.
0
 
nisarkhanAuthor Commented:
my form tag looks like this:

<form id="frm" method="post" runat="server">


and i put the above code in .vb file
0
 
hendridmCommented:
Not sure what's wrong then.  Can you post the full code?
0
 
nisarkhanAuthor Commented:
If Not Page.IsPostBack Then

            Dim i As Integer

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

            For i = 0 To Convert.ToInt16(ddlRatingScalesNumbers.SelectedItem.Text)
                Dim tb As TextBox = New TextBox
                tb.ID = "textbox_" & i
                tb.Text = i.ToString()
                tb.Width = Unit.Pixel(60)

                Me.Controls.Add(New LiteralControl("Textbox #" & i.ToString() & ": "))
                Me.Controls.Add(tb)
                Me.Controls.Add(New LiteralControl("<br />"))
            Next
end if


and my aspx form name is:

<form id="frm" method="post" runat="server">



0
 
hendridmCommented:
Change the last three Me.Controls.Add to frm.Controls.Add.

You're adding them to the page, not to the form (which is where they belong).
0
 
nisarkhanAuthor Commented:
if i change it frm me.controls.add to frm.controls.add

then i get this error:

c:\inetpub\wwwroot\test.aspx.vb(72): Name 'frm' is not declared.
0
 
hendridmCommented:
Regarding your new error, I'm not sure what the problem could be as I don't use codebehind.  Sorry :/

You'll also want to change your 'If Not Page.IsPostBack Then' line to:
 If Page.IsPostBack Then

(ie, remove the 'Not' - that was an error in my first post)
0
 
nisarkhanAuthor Commented:
yes i removed the "NOT"

but if i put back the "frm.controls..." then i get the error / Name frm is not declared

and if i put "ME.Controls..." then i execute  fine but at the end i get the error:

Control 'textbox_0' of type 'TextBox' must be placed inside a form tag with runat=server.


also i added Microsoft.Web.UI.WebControls in references

actually, if i able to create form on .vb then ur code will execute just fine

but do u know how to create form in vb.net

i remember in old days vb, we use to say

dim myfrm as new form

0
 
nisarkhanAuthor Commented:
also i add references

system.windows.forms.dll

but no luck
0
 
sachitjainCommented:
Algo

Read the number (say 3 or 7)
Generate dynamic ids accordingly into db
Select those ids into dataset say(dst)
Bind dataset to datalist
Hide id column for each row in datalist, show only textbox
Let the user fill the textboxes
On submit, fetch values of all textboxes and store them against relevant ids

*********Read the comments below to get more idea about above given algo

First point is how are u going to save those values into the database. I hope that u must be saving those values against dynamically generated ids. Suppose user selects 3 then first create 3 ids say Id1, Id2 and Id3 in the desired table. Once u have got three ids. Then select the 3 rows with these ids and bind the datareader with the datalist that could be declared like this

<asp:datalist id="lstComments" ItemStyle-HorizontalAlign="left" ItemStyle-Width="100%" CellPadding="0" RepeatColumns="1" RepeatDirection="Vertical" CellSpacing="0" Runat="server" Width="100%">                              
              <ItemTemplate>                                                                                                        <asp:TextBox ID="txtComments" Runat="server" CssClass="bodytext" TextMode="MultiLine" Rows="10" Columns="100" Size="100"></asp:TextBox>                                          </ItemTemplate>                                                                                    </asp:datalist>

Bind the data reader with this datalist. It would automatically display 3 rows. Write ItemDataBound event for datalist to hide the id u have selected for each row. Suppose u fetch the rows from db using a dataset (say dst). Then put that dataset in cache or session. User can feed values in those 3 rows and then on click of submit button. You can execute the code something like this

for(int i=0; i<this.lstNotifications.Items.Count; i++)
{
      TextBox txtComments = (System.Web.UI.WebControls.TextBox)this.lstComments.Items[i].FindControl("txtComments");
      string comments = txtComments.Text;
        string id = dst.Tables[0].Rows[i]["id"].ToString();
        ////////////////////Your code to update desired table using comments and id variable
}

Remove dataset dst from cache/session
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

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