locked
dropdownlist RRS feed

  • Question

  • User736276958 posted

    hi all

    when user selects the item from dropdownlist ,based on that item a particular table data from database has to be displayed

    for example in my databa se iam having 3 tables say a,b,c

    if user selects "a"  from dropdownlist ,the data from table "a " has to be displayed in gridview

    if user selects "b"  from dropdownlist ,the data from table "b " has to be displayed in gridview

    .

    .

    .

    .

    .

    for this plz tell me the code

    Thursday, February 12, 2015 4:56 AM

Answers

  • User1577371250 posted

    Hi,

    1. Add a DropDownList with the items, Set AutoPostBack="True" and Add the SelectIndexChange Event

        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="61px">
                <asp:ListItem>a</asp:ListItem>
                <asp:ListItem>b</asp:ListItem>
                <asp:ListItem>c</asp:ListItem>
            </asp:DropDownList>
    
    
            <asp:GridView ID="GridView1" runat="server"></asp:GridView>

    2. Write the Code in Page

    protected void Page_Load(object sender, EventArgs e)
     {
        if (!IsPostBack)
        {
                    // Construct the query with the required COLUMNS
                    // get the Data From Table1 and Bind the GridView
                    GridView1.DataSource = "";
                    GridView1.DataBind();
         }
     }

      protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (DropDownList1.SelectedValue == "a")
                {
    // Construct the query with the required COLUMNS // get the Data From Table1 and Bind the GridView GridView1.DataSource = ""; GridView1.DataBind(); } if (DropDownList1.SelectedValue == "b") { // get the Data From Table2 and Bind the GridView GridView1.DataSource = ""; GridView1.DataBind(); } if (DropDownList1.SelectedValue == "c") { // get the Data From Table3 and Bind the GridView GridView1.DataSource = ""; GridView1.DataBind(); } }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 12, 2015 6:14 AM