c#中的页脚模板下拉列表
本文关键字:下拉列表 | 更新日期: 2023-09-27 18:20:10
我需要创建一个cascading DropDownList in ASP.Net c#
。
第二个DropDownList中的值取决于在第一个DropDownList中选择的值。
我的GridView.
中的Footer Template
中需要这个
当从第一个DropDownList中选择一个值时,我需要用First_DDL_SelectedIndexChanged
事件中执行的查询sql3
的输出填充第二个DropDownCList。
在debug
中,在第一个DropDownList中选择的输出值和查询sql3
上的输出是正确的。
我尝试过使用此解决方案,但没有成功,因为我没有错误,但Second_DDL
始终为空,即使您在First_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();
}
}
}
试试这个:
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();
}
}
这个在你的代码后面:
protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList TRZ = (DropDownList)sender;
GridViewRow row = (GridViewRow)TRZ.NamingContainer;
DRetrieveSubCategories(TRZ.SelectedValue.Substring(0, 3)); //rebind second ddl
DropDownList Second_DDL = (DropDownList)row.FindControl("Second_DDL");
Second_DDL.SelectedIndex = 0;
Second_DDL.Items.Clear();
Second_DDL.DataTextField = "Second_DDL";
Second_DDL.DataValueField = "Second_DDL";
Second_DDL.DataSource = dtSubCategories;
Second_DDL.DataBind();
}