如何根据下拉列表中的选择在gridview中显示数据

时间:2013-03-28 07:37:35

标签: asp.net gridview drop-down-menu datatable

我有一个这样的下拉列表:

<asp:DropDownList ID="ddlPortal2" runat="server" AutoPostBack="True">
        <asp:ListItem>Select</asp:ListItem>
        <asp:ListItem>TRAVELONG</asp:ListItem>
        <asp:ListItem>ONETRAVEL</asp:ListItem>
    </asp:DropDownList>

和带有sqlconnection的网格视图:

 <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
        GridLines="None" onrowcommand="GridView1_RowCommand" 
        AutoGenerateColumns="False">
        <Columns> 
       <asp:BoundField DataField="Portal" HeaderText="Portal" />
        <asp:BoundField DataField="TID" HeaderText="TID" />
        <asp:BoundField DataField="PNR" HeaderText="PNR" />
        <asp:BoundField DataField="TicketNumber" HeaderText="Ticket Number" />
        <asp:BoundField DataField="ESACCode" HeaderText="ESACCode" />
        <asp:BoundField DataField="WaiverCode" HeaderText="WaiverCode" />
        <asp:BoundField DataField="Remarks" HeaderText="Remarks" />
        <asp:BoundField DataField="UnusedTicketAmount" HeaderText="UnusedTicketAmount" />
        <asp:BoundField DataField="ddlUnusedAmount" HeaderText="ddlUnusedAmount" />
        <asp:BoundField DataField="AirlinePenality" HeaderText="AirlinePenality" />
        <asp:BoundField DataField="ddlAirlinePenality" HeaderText="ddlAirlinePenality" />
        <asp:BoundField DataField="NetRefundProcess" HeaderText="NetRefundProcess" />
        <asp:BoundField DataField="ddlNetRefundProcess" HeaderText="ddlNetRefundProcess" />
        <asp:BoundField DataField="RefundableCommission" HeaderText="RefundableCommission" />
        <asp:BoundField DataField="ddlRefundableCommission" HeaderText="ddlRefundableCommission" />
        <asp:BoundField DataField="CouponRefunded" HeaderText="CouponRefunded" />
        <asp:BoundField DataField="RefundType" HeaderText="RefundType" /> 
    </Columns>       
 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:BartConnectionString %>" 
        SelectCommand="SELECT select * [Test]">
        </asp:SqlDataSource>

现在,当我从下拉列表中选择Travelong并按下搜索按钮时,gridview中显示的所有数据都可以告诉我如何操作。 我怎样才能做到这一点 提前谢谢

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

使用SqlDataSource的SelectParameters属性

<asp:DropDownList ID="ddlPortal2" runat="server" AutoPostBack="True">
    <asp:ListItem>Select</asp:ListItem>
    <asp:ListItem>TRAVELONG</asp:ListItem>
    <asp:ListItem>ONETRAVEL</asp:ListItem>
</asp:DropDownList>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:BartConnectionString %>" 
    SelectCommand="SELECT select * [Test] WHERE Portal = @Portal"">
      <selectparameters>
          <asp:controlparameter name="Portal" controlid="ddlPortal2" propertyname="SelectedValue"/>
      </selectparameters>
  </asp:sqldatasource>

Reference

答案 2 :(得分:0)

我得到了答案

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter();
    SqlConnection con;
    if (ddlPortal2.SelectedIndex != 0)
    {
        con = new SqlConnection("Data Source=.;Initial Catalog=Bart;Integrated Security=True");
        con.Open();
        da = new SqlDataAdapter("select * from Test where Portal='"+ddlPortal2.SelectedValue+"' and CONVERT(VARCHAR(25), [DateTime], 101) between '" + tbFrom.Text + "' and '" + tbTo.Text + "'", con);
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        con.Close();
    }
    else if (ddlPortal2.SelectedValue == "All")
    {
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Bart;Integrated Security=True");
        conn.Open();
        da = new SqlDataAdapter("Select * from Test where Portal=Portal", conn);
        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0];
        GridView1.DataBind();
        conn.Close();
    }