C# 从带有ID的下拉列表中选择一个值,并显示在gridview中选择的ID
我的问题是,我只能选择第一个索引,但我想选择下拉列表中的所有值。我尝试了selectedvalue或selectedindexchanged的多个组合,但仍然不起作用C# 从带有ID的下拉列表中选择一个值,并显示在gridview中选择的ID,c#,asp.net,C#,Asp.net,我的问题是,我只能选择第一个索引,但我想选择下拉列表中的所有值。我尝试了selectedvalue或selectedindexchanged的多个组合,但仍然不起作用 <%@ Page Language="C#" Debug="true"%> <%@ import Namespace="System.Data" %> <%@ import Namespace="System.Data.SqlClient" %> <!DOCTYPE html> &
<%@ 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>
string str=“数据源=;uid=sa;pwd=123;数据库=InventoryRouting”;
受保护的无效页面加载(对象发送方、事件参数e)
{
SqlConnection con=新的SqlConnection(str);
string com=“从工厂选择*”;
SqlDataAdapter adpt=新的SqlDataAdapter(com,con);
DataTable dt=新的DataTable();
调整填充(dt);
drop.DataSource=dt;
drop.DataBind();
drop.DataTextField=“PlantID”;
drop.DataValueField=“PlantID”;
drop.DataBind();
}
受保护的无效按钮1\u单击1(对象发送者,事件参数e)
{
SqlConnection con=新的SqlConnection(str);
SqlCommand cmd=new SqlCommand(“从PlantID='”+drop.SelectedValue+'”,con的工厂中选择*);
SqlDataAdapter Adpt=新的SqlDataAdapter(cmd);
DataTable dt=新的DataTable();
调整填充(dt);
grid.DataSource=dt;
grid.DataBind();
}
工厂信息
选择Plant ID以查看信息
如何从下拉列表中选择所有ID?请帮助我在数据绑定之前,您需要检查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();
}
}
在下拉列表中,您一次只能选择一个值。哦,是的,可以,但即使我从运行代码的启动程序中选择另一个值,它仍然不起作用。例如:我从数据库中选择了索引2,但它不显示。你能帮我吗?