0

我的问题是我只能选择第一个索引,但我想选择下拉列表中的所有值。我尝试了 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?请帮我

4

1 回答 1

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();
    }
}
于 2013-05-12T07:20:03.310 回答