skip to Main Content

I want to display N numbers of table in ASP.NET webform based on user input.

For example,

user input -> PDO123045,PDO122111

In this case, user input two values seperated by commas, so if I split the comma, I get length = 2. which are (PDO123045,PDO122111) in a list.
So I want to display two tables in asp webform.

2nd example,

user input -> PDO123045,PDO122111,PDO111345

In this case, user input two values seperated by commas, so if I split the comma, I get length = 3. which are (PDO123045,PDO122111,PDO111345) in a list.
So I want to display three tables in ASP.NET webform since user input three values.

I want to display N numbers of table in ASP.NET webform based on user input.

2

Answers


  1. Ok, so you can do it this way:

    I don’t have your data, but let’s say we are to type in 1 city, or several.

    And for each city we type in (separated by a comma), then we are to display a table for each city.

    So, our markup can be this:

            Enter City(s) for Hotels: 
            <asp:TextBox ID="txtCity" runat="server" Width="500px">
            </asp:TextBox>
    
            <asp:Button ID="cmdShow" runat="server" 
                Text="Show City(s)"
                style="margin-left:30px"
                OnClick="cmdShow_Click"
                />
    
            <asp:Repeater ID="Repeater1" runat="server"
               OnItemDataBound="Repeater1_ItemDataBound"  >
                <ItemTemplate>
                    <div style="width: 50%">
                        <h3><%# $"Hotels For City = {Eval("City")}" %></h3>
                        <asp:GridView ID="GridView1"
                            runat="server" CssClass="table">
                        </asp:GridView>
                    </div>
                </ItemTemplate>
            </asp:Repeater>
    

    And our code behind can be this:

    Protected Sub cmdShow_Click(sender As Object, e As EventArgs)
    
        Dim rstMyCities As New DataTable
        rstMyCities.Columns.Add("City")
    
        Dim sCity As String = txtCity.Text.Replace(" ", "")
        For Each City As String In sCity.Split(",")
            Dim OneRow = rstMyCities.NewRow
            OneRow("City") = City
            rstMyCities.Rows.Add(OneRow)
        Next
    
        Repeater1.DataSource = rstMyCities
        Repeater1.DataBind()
    
    End Sub
    
    Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
    
        If e.Item.ItemType = ListItemType.Item Or
                e.Item.ItemType = ListItemType.AlternatingItem Then
    
            Dim MyBindData As DataRowView = e.Item.DataItem
    
            Dim cmdSQL As New SqlCommand(
                "SELECT FirstName, LastName, City, HotelName, Description FROM tblHotelsA
                WHERE City = @City ORDER BY FirstName ")
    
            cmdSQL.Parameters.Add("@City", SqlDbType.NVarChar).Value = MyBindData("City")
    
            Dim GV As GridView = e.Item.FindControl("GridView1")
            GV.DataSource = MyRstP(cmdSQL)
            GV.DataBind()
    
        End If
    
    End Sub
    

    And the result is this:

    enter image description here

    And I used a helper routine (one gets VERY tired very fast typing over and over connection string code).

    Hence, MyRstP simple returns a data table.

    Public Function MyRstP(cmdSQL As SqlCommand) As DataTable
    
    
        Dim rstData As New DataTable
        Using conn As New SqlConnection(My.Settings.TEST4)
            Using (cmdSQL)
                cmdSQL.Connection = conn
                conn.Open()
                rstData.Load(cmdSQL.ExecuteReader)
            End Using
        End Using
    
        Return rstData
    
    End Function
    
    Login or Signup to reply.
  2. You can use StringBuilder to compose it as HTML table and fill it into a placeholder.

    So first, you gonna add a placeholder into your front end:

    <asp:PlaceHolder ID="ph1" runat="server"></asp:PlaceHolder>
    

    Then at your code behind:

    // split your input by comma
    string[] stringArray = TextBox1.Text.Split(',');
    
    StringBuilder sb = new StringBuilder();
    
    foreach(var str in stringArray)
    {
        sb.Append($"<h2>{str}</h2>");
    
        // Now, build the appropriate table according to your data
        sb.Append($@"
    <table>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
        <tr>
            <td>Data 1</td>
            <td>Data 2</td>
        </tr>
    </table>");
    }
    
    // now display all tables to front end
    ph1.Controls.Add(new LiteralControl(sb.ToString()));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search