从下拉列表中选择一个带有ID的值,并显示在gridview中选择的ID

时间:2013-05-12 07:13:25

标签: c# asp.net

我的问题是我只能选择第一个索引,但我想在下拉列表中选择所有值。我尝试了selectedvalue或selectedindexchanged的多种组合,但它仍无效。

<%@ Page Language="C#" Debug="true"%>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link rel="text/css" href="style1.css"/>
  <script language="c#" runat="server">
      string str = "Data Source=.;uid=sa;pwd=123;database=InventoryRouting";
          protected void Page_Load(object sender, EventArgs e)
          {
              SqlConnection con = new SqlConnection(str);
              string com = "Select * from Plant";
              SqlDataAdapter adpt = new SqlDataAdapter(com, con);
              DataTable dt = new DataTable();
              adpt.Fill(dt);
              drop.DataSource = dt;
              drop.DataBind();
              drop.DataTextField = "PlantID";
              drop.DataValueField = "PlantID";
              drop.DataBind();
          }
          protected void Button1_Click1(object sender, EventArgs e)
          {
              SqlConnection con = new SqlConnection(str);
              SqlCommand cmd = new SqlCommand("select * from Plant where PlantID = '"+     drop.SelectedValue +"'", con);
              SqlDataAdapter Adpt = new SqlDataAdapter(cmd);
              DataTable dt = new DataTable();
              Adpt.Fill(dt);
              grid.DataSource = dt;
              grid.DataBind();
          }
      </script>
</head>
<body>
    <center>
        <h1 style="background-color:red; width:500px;height:50px;">Plant     Information</h1><hr>
    <form id="form1" runat="server">
    <div id="right">
        Choose Plant ID to View Information<asp:DropDownList ID="drop" runat="server"     Width="100px" AutoPostBack="true" OnSelectedIndexChanged="Button1_Click1">    </asp:DropDownList>
        <br />
        <br />
        <!--<asp:Button ID="Button1" runat="server" Text="View"     OnClick="Button1_Click1" Style="height: 26px" />-->
        <br />
        <br />
        <asp:GridView ID="grid" runat="server" BackColor="White" BorderColor="#999999"
            BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center"     />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />
        </asp:GridView>
    </div>
    </form>
        </center>
</body>
</html>

如何从下拉列表中选择所有ID?请帮帮我

1 个答案:

答案 0 :(得分:1)

您需要在数据绑定之前检查Page.IsPostBack属性。当您单击按钮时,页面将回发,如果您没有放置该条件,则将再次加载新数据。你将失去选择

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack){ // add this line 
        SqlConnection con = new SqlConnection(str);
        string com = "Select * from Plant";
        SqlDataAdapter adpt = new SqlDataAdapter(com, con);
        DataTable dt = new DataTable();
        adpt.Fill(dt);
        drop.DataSource = dt;
        drop.DataBind();
        drop.DataTextField = "PlantID";
        drop.DataValueField = "PlantID";
        drop.DataBind();
    }
}