building dynamic list of web from controls

Discussion in 'ASP .Net' started by =?Utf-8?B?c2NvdHRybQ==?=, Apr 18, 2005.

  1. Is there a way to generate a list of say textbox controls dynamically at run
    time, based on say a value coming out of a database which could vary each
    time the code is run. In traditional asp you could just have run a loop to do
    something like that. I am aware in you can use something like
    Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but you
    still need to declare each individual textbox control as far as I can tell..
    This is not practial as I don't know how many I will need before I run the
    page.


    --
    Scott
     
    =?Utf-8?B?c2NvdHRybQ==?=, Apr 18, 2005
    #1
    1. Advertising

  2. =?Utf-8?B?c2NvdHRybQ==?=

    Karl Seguin Guest

    dim x as integer = 10 'pull this from db

    for i = 1 to 10
    dim txt as new TextBox()
    txt.Id = i.ToString()
    Form1.Controls.Add(txt)
    end

    Karl

    --
    MY ASP.Net tutorials
    http://www.openmymind.net/ - New and Improved (yes, the popup is
    annoying)
    http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
    come!)
    "scottrm" <> wrote in message
    news:...
    > Is there a way to generate a list of say textbox controls dynamically at

    run
    > time, based on say a value coming out of a database which could vary each
    > time the code is run. In traditional asp you could just have run a loop to

    do
    > something like that. I am aware in you can use something like
    > Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but

    you
    > still need to declare each individual textbox control as far as I can

    tell..
    > This is not practial as I don't know how many I will need before I run the
    > page.
    >
    >
    > --
    > Scott
     
    Karl Seguin, Apr 18, 2005
    #2
    1. Advertising

  3. =?Utf-8?B?c2NvdHRybQ==?=

    Scott Allen Guest

    Perhaps you could add the TextBox controls to a higher level parent
    control, like a Panel or even a Repeater. You wouldn't have to declare
    each TextBox control in the code behind as a class member variable -
    you could loop through the controls in the container and act upon
    them.

    Making sense?

    --
    Scott
    http://www.OdeToCode.com/blogs/scott/

    On Mon, 18 Apr 2005 07:11:13 -0700, "scottrm"
    <> wrote:

    >Is there a way to generate a list of say textbox controls dynamically at run
    >time, based on say a value coming out of a database which could vary each
    >time the code is run. In traditional asp you could just have run a loop to do
    >something like that. I am aware in you can use something like
    >Form1.Controls.Add(TextBox1) to add textboxes dynamically at run time but you
    >still need to declare each individual textbox control as far as I can tell..
    >This is not practial as I don't know how many I will need before I run the
    >page.
     
    Scott Allen, Apr 18, 2005
    #3
  4. =?Utf-8?B?c2NvdHRybQ==?=

    Brock Allen Guest

    Yep, it's fairly easy. The big problem is that across postbacks it's up to
    you to recreate the controls. I just posted a sample earlier:

    http://groups-beta.google.com/group... and dynamic controls&rnum=1#a5717a97bd32c450

    -Brock
    DevelopMentor
    http://staff.develop.com/ballen



    > Is there a way to generate a list of say textbox controls dynamically
    > at run time, based on say a value coming out of a database which could
    > vary each time the code is run. In traditional asp you could just have
    > run a loop to do something like that. I am aware in you can use
    > something like Form1.Controls.Add(TextBox1) to add textboxes
    > dynamically at run time but you still need to declare each individual
    > textbox control as far as I can tell.. This is not practial as I don't
    > know how many I will need before I run the page.
    >
     
    Brock Allen, Apr 18, 2005
    #4
  5. Thanks for all your inputs,

    Hi Scott,

    If the number of textboxes are not constant and depend on the datasoure's
    record counts, I recommend that you consider using a template databound
    control (specifying the textbox in the item template), such as
    Repeater/DataList to display them. Also, it's not very complex to build a
    custom template databound control so as to meet our particular requirement.

    If there is any further quesitons, please feel free to post here. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 19, 2005
    #5
  6. Hi

    Thanks for your reply. Using a datalist with a textbox in the item template
    is fine but there a couple of other things I need to do which I can't seem to
    find much information on

    1. When I have a list of textboxes based on values from the database I need
    to populate the content of each with data, when I try something like the code
    below I get an error.

    <asp:DataList id="DataList1" runat="server">
    <ItemTemplate>
    <asp:TextBox id="TextBox1" runat="server"><%#
    Container.DataItem("Name")%></asp:TextBox>
    </ItemTemplate>
    </asp:DataList>

    2. When the user fills in all the text boxes and hits submit I need to
    retrieve all those values to store in a database.


    Steven Cheng[MSFT]" wrote:

    > Thanks for all your inputs,
    >
    > Hi Scott,
    >
    > If the number of textboxes are not constant and depend on the datasoure's
    > record counts, I recommend that you consider using a template databound
    > control (specifying the textbox in the item template), such as
    > Repeater/DataList to display them. Also, it's not very complex to build a
    > custom template databound control so as to meet our particular requirement.
    >
    > If there is any further quesitons, please feel free to post here. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
     
    =?Utf-8?B?c2NvdHRybQ==?=, Apr 22, 2005
    #6
  7. Hi Scott,

    Glad to hear from you. As for the further problems you mentioned, here are
    some of my suggestions:

    1. The error you met when binding text with textbox like:
    ==================
    <asp:TextBox id="TextBox1" runat="server"><%#
    Container.DataItem("Name")%></asp:TextBox>
    =====================

    is because the TextBox can't didn't support any content in it's InnerTag.
    We should set the binding expression for it's Text property. For example:

    <asp:TextBox id="TextBox1" runat="server"
    Text="<%# Container.DataItem('Name')%>"></asp:TextBox>


    2. As for how to retrieve all the input values in the textboxes in the
    DAtaList control. We can use the Items collection to loopthrough all the
    Items in DataList and find the TextBox in each itemtemplate and retrieve
    the text value(we need to specify an ID for TextBox so as to reference it).
    For example:


    foreach(DataListItem dli in dlMain.Items)
    {
    TextBox txt = dli.FindControl("txtValue") as TextBox;
    if(txt != null)
    {
    //do something
    }
    }

    If there are anything else unclear, please feel free to post here. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 25, 2005
    #7
  8. Thanks for the reply, that answers my question 1. ok but I do have a bit of a
    problem with point 2. as I am unclear where to get the id's for the text
    boxes since they are generated dynamically. I tried having a look at the html
    produced and getting it from there eg. "DataList1__ctl157_TextBox1" but that
    did not seem to work

    "Steven Cheng[MSFT]" wrote:

    > Hi Scott,
    >
    > Glad to hear from you. As for the further problems you mentioned, here are
    > some of my suggestions:
    >
    > 1. The error you met when binding text with textbox like:
    > ==================
    > <asp:TextBox id="TextBox1" runat="server"><%#
    > Container.DataItem("Name")%></asp:TextBox>
    > =====================
    >
    > is because the TextBox can't didn't support any content in it's InnerTag.
    > We should set the binding expression for it's Text property. For example:
    >
    > <asp:TextBox id="TextBox1" runat="server"
    > Text="<%# Container.DataItem('Name')%>"></asp:TextBox>
    >
    >
    > 2. As for how to retrieve all the input values in the textboxes in the
    > DAtaList control. We can use the Items collection to loopthrough all the
    > Items in DataList and find the TextBox in each itemtemplate and retrieve
    > the text value(we need to specify an ID for TextBox so as to reference it).
    > For example:
    >
    >
    > foreach(DataListItem dli in dlMain.Items)
    > {
    > TextBox txt = dli.FindControl("txtValue") as TextBox;
    > if(txt != null)
    > {
    > //do something
    > }
    > }
    >
    > If there are anything else unclear, please feel free to post here. Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
    >
    >
     
    =?Utf-8?B?c2NvdHRybQ==?=, Apr 25, 2005
    #8
  9. Thanks for your response Scott,

    As for FindControl, when we call FindConrol on a webControl which is a
    NamingContainer, we don't need to specify the underlying encoded id of the
    sub controls in it. We can directly use the ID we specify in aspx
    template. For the example I mentioned in the previous message:

    when we have the following template
    ===================
    <ItemTemplate>
    <asp:TextBox id="txtValue" runat="server"
    Text="<%# Container.DataItem('Name')%>"></asp:TextBox>

    </ItemTemplate>

    we can just use the below code to retrieve the TextBox's reference:

    foreach(DataListItem dli in dlMain.Items)
    {
    TextBox txt = dli.FindControl("txtValue") as TextBox;
    if(txt != null)
    {
    //do something
    }
    }
    ===================

    the encoded ID such as DataList1__ctl157_TextBox1 is the internal
    implementation of ASP.NET, we should not care about that in a developer's
    general perspective.

    Hope Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 26, 2005
    #9
  10. HI

    Thanks for your help, much appreciated. I do have one final question. Is
    there any way to identify specific text boxes with values based on values
    taken from the database so I can accurately know which text boxes I am
    retrieving data from.

    "Steven Cheng[MSFT]" wrote:

    > Thanks for your response Scott,
    >
    > As for FindControl, when we call FindConrol on a webControl which is a
    > NamingContainer, we don't need to specify the underlying encoded id of the
    > sub controls in it. We can directly use the ID we specify in aspx
    > template. For the example I mentioned in the previous message:
    >
    > when we have the following template
    > ===================
    > <ItemTemplate>
    > <asp:TextBox id="txtValue" runat="server"
    > Text="<%# Container.DataItem('Name')%>"></asp:TextBox>
    >
    > </ItemTemplate>
    >
    > we can just use the below code to retrieve the TextBox's reference:
    >
    > foreach(DataListItem dli in dlMain.Items)
    > {
    > TextBox txt = dli.FindControl("txtValue") as TextBox;
    > if(txt != null)
    > {
    > //do something
    > }
    > }
    > ===================
    >
    > the encoded ID such as DataList1__ctl157_TextBox1 is the internal
    > implementation of ASP.NET, we should not care about that in a developer's
    > general perspective.
    >
    > Hope Thanks,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
     
    =?Utf-8?B?c2NvdHRybQ==?=, Apr 27, 2005
    #10
  11. Thanks for your reply Scott,

    I'm glad that my suggestion was of assistance. As for the identify the
    Textboxes so as to conveniently reference them through the database column
    name in db, I think we can make use of the DataGrid/DataList's ItemCreated
    event, in that event, we can retrieve the control instance and manually
    assign ID to it. For example:

    private void dgPager_ItemCreated(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    ListItemType.AlternatingItem)
    {
    TextBox txt = e.Item.FindControl("txtOld") as TextBox;

    if(txt != null)
    {
    txt.ID = "txtNew";
    }
    }
    }

    In addition, as my own opinion, I don't recommend this means since
    manually assign ID will require additional processing time which may reduce
    the performance. And I think assign the ID at design time in page template
    is the prefer means.

    Thanks,

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 28, 2005
    #11
  12. HI

    Thanks for this although I am not sure I have explained properly, what I
    want to do is say build a list of 10 checkboxes based on a value from the
    database and assign each checkbox with an id based on a database value. I
    then need to retrieve the contents of the texboxes based on the id from the
    database. I am not sure how I would use your code to do this as
    e.Item.FindControl("txtOld") used only one id "txtOld" not multiple ones for
    each text box like I want. Also you say that I could assign the ID at design
    time in the page template but I can only assign a single ID, not identify the
    text boxes individually.

    "Steven Cheng[MSFT]" wrote:

    > Thanks for your reply Scott,
    >
    > I'm glad that my suggestion was of assistance. As for the identify the
    > Textboxes so as to conveniently reference them through the database column
    > name in db, I think we can make use of the DataGrid/DataList's ItemCreated
    > event, in that event, we can retrieve the control instance and manually
    > assign ID to it. For example:
    >
    > private void dgPager_ItemCreated(object sender,
    > System.Web.UI.WebControls.DataGridItemEventArgs e)
    > {
    > if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==
    > ListItemType.AlternatingItem)
    > {
    > TextBox txt = e.Item.FindControl("txtOld") as TextBox;
    >
    > if(txt != null)
    > {
    > txt.ID = "txtNew";
    > }
    > }
    > }
    >
    > In addition, as my own opinion, I don't recommend this means since
    > manually assign ID will require additional processing time which may reduce
    > the performance. And I think assign the ID at design time in page template
    > is the prefer means.
    >
    > Thanks,
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Support
    >
    > Get Secure! www.microsoft.com/security
    > (This posting is provided "AS IS", with no warranties, and confers no
    > rights.)
    >
    >
     
    =?Utf-8?B?c2NvdHRybQ==?=, Apr 28, 2005
    #12
  13. Hi Scott,

    Thanks for your further description. I think I did misunderstand your
    question in the last response. So let me restate my understanding:

    Since you bind the checkbox (or other entry field) control with datas
    retrieved from database records, you want to also identify each checkbox(in
    the datalist items) so as to mapping the checkbox's value with the record
    in database, yes? If there're still anything I misunderstood, please feel
    free to let me know.

    If this is your actual requirement, I think we have the following options:

    1. use the webControl.Attributes collection and store the primary key into
    a certain control's attribute. For example:
    =========================
    private void dlFISVIEW_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataListItemEventArgs e)
    {
    DataRowView drv = e.Item.DataItem as DataRowView;
    CheckBox chk = e.Item.FindControl("chkSelected") as CheckBox;

    if(chk != null && drv != null)
    {
    chk.Attributes["id"] = drv["id"];
    }
    }
    =======================

    Then, when we retrieve the CheckBox later in postback event, we can check
    its Attirubtes["id"] 's value to identify it.

    2. If you don't want to use the Attributes collection. We can also consider
    adding an additional HIDDEN control into the template to store the identity
    of the data record, for example:

    <itemTemplate>
    <asp:Label id="key" runat="server" Visible="false" ..../>
    <asp:CheckBox..... >
    </itemtemplate>


    And in ItemDataBound, we can store the primary key or other identity into
    the label control's text and retrieve it later.

    How do you think of this? If you have any other questions, please feel free
    to post here. Thanks,

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Apr 28, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SXJmYW4gQWtyYW0=?=

    Dynamic Controls in asp.net...Building a tree hierarchy..please he

    =?Utf-8?B?SXJmYW4gQWtyYW0=?=, Dec 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    506
    =?Utf-8?B?SXJmYW4gQWtyYW0=?=
    Dec 13, 2004
  2. Ronald
    Replies:
    2
    Views:
    847
    Robbe Morris [C# MVP]
    Jan 17, 2006
  3. Robert Bevington

    Building Dynamic Controls

    Robert Bevington, Jul 11, 2007, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    727
    Robert Bevington
    Jul 11, 2007
  4. Denny Smolinski via .NET 247

    Dynamic Controls, Placeholder, Retrieve Information from Dynamic Controls

    Denny Smolinski via .NET 247, May 4, 2004, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    469
    Eidolon
    May 4, 2004
  5. Irfan Akram
    Replies:
    1
    Views:
    161
    Mujtaba Syed
    Dec 13, 2004
Loading...

Share This Page