我正在尝试使用针对下拉列表更改的下拉列表选定索引的查询从sql数据库显示gridview。
以下是我目前的代码,我认为应该可行:
我没有收到任何错误,当我从下拉列表中选择一个包时,gridview就不会出现。
C#:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DropDownList1.Items.Clear();
DropDownList1.Items.Add(new ListItem("Select Package", "Select Package"));
DropDownList1.AppendDataBoundItems = true;
String strConnString = ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
String strQuery = "select distinct Package_Name from dbo.PackageDetail";
SqlConnection con = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = strQuery;
cmd.Connection = con;
try
{
con.Open();
DropDownList1.DataSource = cmd.ExecuteReader();
DropDownList1.DataTextField = "Package_Name";
DropDownList1.DataBind();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
con.Dispose();
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ID = "SqlDataSource1";
this.Page.Controls.Add(SqlDataSource1);
SqlDataSource1.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["datarepConnectionString"].ConnectionString;
SqlDataSource1.SelectCommand = "SELECT [ID], [PartNumber], [Qty], [PartDescription], [PR1], [PR2], [PR3], [EXTPR2], [EXTPR1], [EXTPR3] FROM [PackageDetail] where [Package_Name] = '" + DropDownList1.Text + "' ";
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
ASP.NET:
<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True" EnableViewState="true" >
</asp:DropDownList>
</center>
<p> </p>
<p>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ID" ForeColor="Black" GridLines="Vertical" AllowSorting="True" Height="160px" Width="374px" EnableViewState="true" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="PartNumber" HeaderText="PartNumber" SortExpression="PartNumber" />
<asp:BoundField DataField="PartDescription" HeaderText="PartDescription" SortExpression="PartDescription" />
<asp:BoundField DataField="Qty" HeaderText="Qty" SortExpression="Qty" />
<asp:BoundField DataField="PR1" HeaderText="PR1" SortExpression="PR1" />
<asp:BoundField DataField="PR2" HeaderText="PR2" SortExpression="PR2" />
<asp:BoundField DataField="PR3" HeaderText="PR3" SortExpression="PR3" />
<asp:BoundField DataField="EXTPR2" HeaderText="EXTPR2" SortExpression="EXTPR2" />
<asp:BoundField DataField="EXTPR1" HeaderText="EXTPR1" SortExpression="EXTPR1" />
<asp:BoundField DataField="EXTPR3" HeaderText="EXTPR3" SortExpression="EXTPR3" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
非常感谢帮助。谢谢。