We help IT Professionals succeed at work.
Get Started
Troubleshooting Question

ASP.Net dynamically add custom gridview

76 Views
Last Modified: 2020-11-10
ASP single page web project:
User selects a Query {1,2,3} then clicks "Button". Depending on Query selected, SQL is run which returns data which I would like displayed in a table where PlaceHolder is.

I'm very good at HTML, CSS, PHP, C#. New to ASP.Net

So far I have WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:RadioButtonList ID="RadioButtonList1" runat="server">
                <asp:ListItem Value="1">Query 1</asp:ListItem>
                <asp:ListItem Value="2">Query 2</asp:ListItem>
                <asp:ListItem Value="3">Query 3</asp:ListItem>
            </asp:RadioButtonList>
        </div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <p>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        </p>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
    </form>
</body>
</html>
WebForm.aspx.cs
using System;

namespace WebApplication5
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "Selected: " + RadioButtonList1.SelectedItem.ToString();
            switch (RadioButtonList1.SelectedValue)
            {
                case "1":
                    // Query 1
                    break;
                case "2":
                    // Query 2
                    break;
                case "3":
                    // Query 3
                    break;
            }
        }
    }
}
The GridView I would like to insert into PlaceHolder looks like (it would be really cool if the columns were auto generated, but I'll settle for manually specifying them like this for now):
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" SelectCommand="SELECT * FROM [MyTable]"></asp:SqlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
    <Columns>
        <asp:BoundField DataField="Customer Name" HeaderText="Customer Name" />
        <asp:BoundField DataField="Address" HeaderText="Address" />
    </Columns>
</asp:GridView>
I'll have 2 other GridViews defined similar to this one for Query 2 and Query 3.

My question now is, how do I piece this together? I looked into making a user control, but it appeared to involve creating a separate project for each user control, which seemed too complex I want to keep this simple so someone else later on can easily understand this (which is why I'm veering away from MVC as that seems too complex for this simple project.)

Thank you!

Comment
Watch Question
Sr. System Analyst
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE