【发布时间】:2014-10-24 19:46:47
【问题描述】:
我需要创建一个cascading DropDownList in ASP.Net c#。
第二个 DropDownList 中的值取决于第一个 DropDownList 中选择的值。
我的 GridView.
Footer Template 需要这个
当从第一个 DropDownList 中选择一个值时,我需要使用 First_DDL_SelectedIndexChanged 事件中执行的查询 sql3 的输出填充第二个 DropDownList。
在 debug 中,第一个 DropDownList 中选择的输出值和查询 sql3 中的输出是正确的。
我尝试使用此解决方案但没有成功,因为我没有错误,但即使您选择 First_DDL 中的值,Second_DDL 也始终为空强>。
非常感谢您在解决这个问题时给我的任何帮助。
这是我的代码:
using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Level_Default2 : System.Web.UI.Page
{
OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString);
DataSet dset;
DataTable dt = new DataTable();
DataTable dtCategories = new DataTable();
DataTable dtSubCategories = new DataTable();
OdbcDataAdapter dadapter;
private DataTable RetrieveSubCategories(string TRZ)
{
string sql3 = " SELECT ... where TRZ = ?; ";
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql3, cn))
{
dadapter = new OdbcDataAdapter(cmd);
dadapter.SelectCommand.Parameters.AddWithValue(?, TRZ.SelectedItem.ToString().Substring(0, 3));
dadapter.Fill(dtSubCategories);
}
}
return dtSubCategories;
}
private DataTable RetrieveCategories()
{
string sql2 = " SELECT ... ; ";
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
cn.Open();
using (OdbcCommand cmd = new OdbcCommand(sql2, cn))
{
dadapter = new OdbcDataAdapter(cmd);
dadapter.Fill(dtCategories);
}
}
return dtCategories;
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList First_DDL = (DropDownList)e.Row.FindControl("First_DDL");
First_DDL.DataTextField = "First_DDL";
First_DDL.DataValueField = "First_DDL";
First_DDL.DataSource = RetrieveCategories();
First_DDL.DataBind();
DropDownList Second_DDL = (DropDownList)e.Row.FindControl("Second_DDL");
Second_DDL.DataTextField = "Second_DDL";
Second_DDL.DataValueField = "Second_DDL";
Second_DDL.DataSource = dtSubCategories;
Second_DDL.DataBind();
}
}
protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList TRZ = (DropDownList)sender;
GridViewRow row = (GridViewRow)TRZ.NamingContainer;
RetrieveSubCategories(TRZ.SelectedItem.ToString().Substring(0, 3)); //rebind second ddl
Response.Write(TRZ.SelectedItem.ToString().Substring(0, 3));
}
public DataTable GridViewBind()
{
using (OdbcConnection cn =
new OdbcConnection(ConfigurationManager.ConnectionStrings["cn"].ConnectionString))
{
string sql1 = " SELECT ... ; ";
using (OdbcDataAdapter command =
new OdbcDataAdapter(sql1, cn))
{
cn.Open();
dset = new DataSet();
dset.Clear();
command.Fill(dset);
DataTable dt = dset.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
return dt;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridViewBind();
}
}
}
【问题讨论】:
标签: c# asp.net gridview footer