Assigning Value to Programmatically Added Textbox(es)

Rate It (2)

Last post 04-20-2009 2:24 AM by mmmisa83. 5 replies.

Sort Posts:

  • Assigning Value to Programmatically Added Textbox(es)

    04-15-2009, 10:53 PM
    • Member
      9 point Member
    • mmmisa83
    • Member since 04-03-2007, 8:33 AM
    • Posts 12

    Hi,

    I have this problem:

    This program adds a number of textboxes (depends on intTotal given by user):

    For i = 0 To intTotal - 1
      Dim newTextBox As New TextBox
      newTextBox.ID = "txtDetail" & (i + 1).ToString
      form1.Controls.Add(newTextBox)
    Next

    The above code works fine. But when it comes to assigning a value to each textbox in other function, I have no idea. Here is what I want to do:

    If intTotal = 1
       txtDetail1 = "1"
    If intTotal = 2
       txtDetail1 = "1"
       txtDetail2 = "2"
    and so forth..

    I'm thinking of using For loop, but how do I name each textbox in code behind when the textbox is created in runtime?

    For i = 0 To Convert.ToInt32(oTotalVar) - 1
       "txtDetail" & i = ??????
    Next


    Thanks in advance  for any help or suggestion.

  • Re: Assigning Value to Programmatically Added Textbox(es)

    04-15-2009, 11:41 PM
    Answer

    Could you try

     For i = 0 To Convert.ToInt32(oTotalVar) - 1
       this.FindControl( "txtDetail" & i).Text = i
    Next

    Cheers

    Paul

    Please mark this post as the answer if you found it useful.

    Cheers

    Paul
  • Re: Assigning Value to Programmatically Added Textbox(es)

    04-15-2009, 11:57 PM
    • Star
      11,839 point Star
    • shahed.kazi
    • Member since 07-09-2008, 2:15 AM
    • Sydney, Australia
    • Posts 2,328
  • Re: Assigning Value to Programmatically Added Textbox(es)

    04-16-2009, 2:10 AM
    Answer
    • Member
      129 point Member
    • dpkngm
    • Member since 03-02-2009, 5:24 AM
    • Noida
    • Posts 28
    Deepak Nigam
    deepak@swiftcybernetics.com
    http://www.swiftcybernetics.com

    please mark as answer if it helps u.
  • Re: Assigning Value to Programmatically Added Textbox(es)

    04-16-2009, 3:03 AM
    Answer
    • Member
      618 point Member
    • seekerwoo
    • Member since 03-24-2009, 3:15 AM
    • Sydney
    • Posts 109

     I'm not quite sure if the following solution is what you want, but using repeater to loop and render is alternative approch.

    1. aspx file

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="dynamictextboxrepeater.aspx.cs" Inherits="ForumTest01.dynamictextboxrepeater" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Repeater ID="Repeater1" runat="server"
                onitemdatabound="Repeater1_ItemDataBound" >
                <HeaderTemplate>
                <table>
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:Literal ID="Literal1" runat="server" Text="<tr>" ></asp:Literal>
                    <asp:Literal ID="Literal2" runat="server" Text="<td>" ></asp:Literal>
          
                    <asp:Literal ID="Literal3" runat="server" Text="</td>" ></asp:Literal>
                    <asp:Literal ID="Literal4" runat="server" Text="</tr>" ></asp:Literal>
                </ItemTemplate>
                <FooterTemplate>
                </table>
                </FooterTemplate>
            </asp:Repeater>
        </div>
        </form>
    </body>
    </html>

    2. code behind

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace ForumTest01
    {
        public partial class dynamictextboxrepeater : System.Web.UI.Page
        {
            protected void Page_prerender(object sender, EventArgs e)
            {
                List<int> lst = new List<int>();
                for (int i = 1; i < 10; i++)
                {
                    lst.Add(i);
                }

                Repeater1.DataSource = lst;
                Repeater1.DataBind();
            }

            protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
                if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
                {
                    TextBox tb = new TextBox();
                    tb.ID = e.Item.DataItem.ToString();
                    e.Item.Controls.AddAt(4, tb);
                }
            }
        }
    }
     

    Jake C
  • Re: Assigning Value to Programmatically Added Textbox(es)

    04-20-2009, 2:24 AM
    • Member
      9 point Member
    • mmmisa83
    • Member since 04-03-2007, 8:33 AM
    • Posts 12

    Thanks. Paul's codes work. Other replies also help. Thanks again.

Page 1 of 1 (6 items)