62,074
社区成员
发帖
与我相关
我的任务
分享
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView_category" AutoGenerateColumns="false" Width="200" runat="server"
OnRowDataBound="GridView_category_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList_category" Width="150" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_category_SelectedIndexChanged" runat="server">
</asp:DropDownList></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:GridView ID="GridView_product" AutoGenerateColumns="false" Width="200" runat="server">
<Columns>
<asp:BoundField HeaderText="产品编号" DataField="ProductId" />
<asp:BoundField HeaderText="产品名称" DataField="ProductName" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.GridView_category.DataSource = GetCategory();
this.GridView_category.DataBind();
}
}
protected void GridView_category_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList DropDownList_category = e.Row.Cells[0].FindControl("DropDownList_category") as DropDownList;
if (DropDownList_category != null)
{
DropDownList_category.DataValueField = "CategoryId";
DropDownList_category.DataTextField = "CategoryName";
DropDownList_category.DataSource = GetCategory();
DropDownList_category.DataBind();
}
}
}
protected void DropDownList_category_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropDownList_category = sender as DropDownList;
string categoryId = DropDownList_category.SelectedValue;
if (!string.IsNullOrEmpty(categoryId))
{
this.GridView_product.DataSource = GetProduct(categoryId);
this.GridView_product.DataBind();
}
}
private DataTable GetCategory()
{
string strConnection = "Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa";
string strSelect = "select CategoryId,CategoryName from Categories";
SqlDataAdapter da = new SqlDataAdapter(strSelect, strConnection);
DataTable table = new DataTable();
try
{
da.Fill(table);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return table;
}
private DataTable GetProduct(string categoryId)
{
string strConnection = "Data Source=(local);Initial Catalog=Northwind;Persist Security Info=True;User ID=sa";
string strSelect = "select ProductId,ProductName from Products where CategoryId="+categoryId;
SqlDataAdapter da = new SqlDataAdapter(strSelect, strConnection);
DataTable table = new DataTable();
try
{
da.Fill(table);
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return table;
}
}
public void zqddl_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ggddl =(DropDownList)this.datagrid.Items[datagrid.EditItemIndex].FindControl("zqddl");
//........
}