skip to Main Content

On an aspx page I have several of the TextBox control rows added like this:

<asp:Panel ID="panel1" runat="server" Visible="true">
    <tr>
         <asp:TextBox ID="txt1" runat="server"</asp:TextBox>
         
         <asp:DropDownList ID="dd1" runat="server">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem></asp:ListItem>
            <asp:ListItem></asp:ListItem>
         </asp:DropDownList>
    </tr>

    <tr>
         <asp:TextBox ID="txt2" runat="server" ></asp:TextBox>
         
         <asp:DropDownList ID="dd2" runat="server">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem></asp:ListItem>
            <asp:ListItem></asp:ListItem>
         </asp:DropDownList>
    </tr>

    <tr>
         <asp:TextBox ID="txt3" runat="server"></asp:TextBox>
         
         <asp:DropDownList ID="dd3" runat="server">
            <asp:ListItem></asp:ListItem>
            <asp:ListItem></asp:ListItem>
            <asp:ListItem></asp:ListItem>
         </asp:DropDownList>
    </tr>
</asp:panel>

The above code works fine, however there are several of these rows, therefore I want to use a loop.

Assume these are 50 rows, I want to display these rows with a loop. I want to display only those rows where text fields are not empty.

<%
    for (int i = 1; i < 50; i++) {

        string txtBox = "txt" + i.ToString();

        if (string.IsNullOrEmpty(txtBox.Text)) {
            //display
        }
    }
 %>

In the above code, I am getting error at txtBox.Text because txtBox is a local variable.
I have also tried this:

TextBox txt = (TextBox)this.FindControl(txtBox);

This does not give error but it returns null.

So my question is that how can I display above controls with a loop instead of typing each control one by one.

2

Answers


  1. You cannot access the id of the text box or other control by defining the variable.
    txtBox is a variable and you treat it like a textbox id.
    To solve this problem, I think you should build your controls entirely with the backend from the beginning. In this case, you can set their value in the loop. But to read their value, you can store their value in a cookie, for example, and then read them.
    You can also use javascript to assign them values

    Login or Signup to reply.
  2. Well, it doesn’t matter if you have 3, or 30 here.

    The simple matter is you want something to "repeat".

    I would suggest a listview, or how about a "repeater" that repeats one chunk of markup over and list view over.

    However, I have to think that some kind of data source is going to drive this data, and thus you want to think of solutions from a database point of view. And even when you list view don’t have a database, you STILL want to think in terms of datasets, or data theory.

    The second issue not addressed here is that combo box, or dropdown list.

    I can’t imagine that the values for those 50 rows is going to be typed in by hand. So, the list for the select values of course would also be from a data source. (Or at the very least some data set from code).

    So, with above in mind, lets use a repeater, and "repeat" the one template.

    Regardless, so say this markup:

    <asp:Repeater ID="Repeater1" runat="server" 
        OnItemDataBound="Repeater1_ItemDataBound">
        <ItemTemplate>
    
            <div style="width:30%">
                <h3><%# Eval("hText") %></h3>
    
                <asp:TextBox ID="txt1" runat="server"
                    Text='<%# Eval("TboxText") %>' >
                </asp:TextBox>
    
                <asp:DropDownList ID="dHotels" runat="server"
                    style= "width:120px;margin-left:5px"                        
                    DataValueField="ID"
                    DataTextField="HotelName">
                </asp:DropDownList>
                <br />
                <hr/>
            </div>
    
        </ItemTemplate>
    </asp:Repeater>
    

    So, from above the key concept is you lay out the "one thing" you wish to repeat, and then simply feed the repeater a data list (or even a list of some class).

    The data source for each row combo (drop downlist) was set one time, and we on the item data bound event, setup the choices for the combo box based on that data table.

    So, code behind now can concentrate on building a data source, and not mess with markup.

    So, then this code:

        DataTable rstHotels = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                LoadData();
        }
    
        void LoadData()
        {
            // combo box data (same for each repeat)
            rstHotels = 
                General.MyRst("SELECT ID, HotelName FROM tblHotelsA ORDER BY HotelName");
    
            int NumRows = 7;   // number of repeat's
    
            DataTable dt = new DataTable();
            dt.Columns.Add("hText", typeof(string));
            dt.Columns.Add("TboxText", typeof(string));
    
            for (int i = 0; i < NumRows; i++)
            {
                DataRow dr = dt.NewRow();
                dr[0] = "Item Text" + i.ToString();
                dr[1] = "TextBox " + i.ToString();
                dt.Rows.Add(dr);
            }
            Repeater1.DataSource = dt;
            Repeater1.DataBind();   
    
        }
    
        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) ||
                (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                DropDownList cboList = (DropDownList)e.Item.FindControl("dHotels");
                cboList.DataSource = rstHotels;
                cboList.DataBind();
                cboList.Items.Insert(0, new ListItem("Select hotel", ""));
            }
        }
    

    So, then your code becomes data operation, and "list" or "set" based operations, and you let the repeater control (or GridView, or ListView) handle the repeating of that one "row" of data or information that you send to that repeater "type" of control. While this example used a "Repeater" control, you have quite a few other choices.

    The result of above is thus:

    enter image description here

    So, those from other code environments tend to use looping to add controls to a page, but as you can see the only looping code required for this SO posting was to create a "fake" data source.

    Same goes for the dropdown list control and choices, they can be data source driven without requiring one to write or even use loops.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search