2013-05-12 50 views
0

我的問題是,我只能選擇第一個索引,但我想選擇下拉列表中的所有值。我嘗試了selectedvalue或selectedindexchanged的多種組合,但它仍然不起作用。從ID下拉列表中選擇一個值,並顯示在gridview中選擇的ID

<%@ 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?請幫助我

+0

在DropdownList中,您一次只能選擇一個值。 – Magnus 2013-05-12 07:16:54

+0

呵呵,沒錯,但即使我從運行代碼的入門者中選擇了其他值,它仍然無法工作。例如:我從我的數據庫中選擇索引2,但它不顯示。你能幫助我嗎? – 2013-05-12 07:19:09

回答

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(); 
    } 
} 
+0

謝謝你的好意...它真的對我有用......:D – 2013-05-12 07:28:01

+0

好的,我明白了......謝謝 – 2013-05-12 07:40:35

相關問題