我需要在ASP.Net c#中创建一个 cascading DropDownList
,它是DropDownList,它依赖于 Footer Template
中数据的第一个DropDownList在我的 GridView
。
当从第一个DropDownList中选择一个值时,我需要使用查询sql3的值填充第二个DropDownList,并在事件 First_DDL_SelectedIndexChanged
中执行。
在 debug 中,在第一个DropDownList和查询sql3中选择的输出值是正确的。
我尝试使用此解决方案但未成功,因为错误是:
Compiler Error Message: CS0103:
The name 'dtSubCategories' does not exist in the current context
在这行代码隐藏中:
Line 360:
Second_DDL.DataSource = dtSubCategories();
我非常感谢您在解决这个问题时能给我的任何帮助。
这是我的代码:
protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList First_DDL = (DropDownList)sender;
GridViewRow row = (GridViewRow)First_DDL.NamingContainer;
Response.Write(First_DDL.SelectedItem.ToString().Substring(0, 3) + "<br /><br />");
sql3 = " SELECT FROM `tbl_m` WHERE fiedl1 = ?; ";
Response.Write(sql3);
DataTable dtSubCategories = new DataTable();
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
myConnectionString.Open();
using (OdbcCommand cmd = new OdbcCommand(sql3, myConnectionString))
{
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
cmd.Parameters.AddWithValue("param1", First_DDL.SelectedItem.ToString().Substring(0, 3));
adapter.Fill(dtSubCategories);
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList Second_DDL = (DropDownList)e.Row.FindControl("Second_DDL");
Second_DDL.DataTextField = "text";
Second_DDL.DataValueField = "text";
Second_DDL.DataSource = dtSubCategories();
Second_DDL.DataBind();
}
}
编辑#1
DataTable dtSubCategories = new DataTable();
protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList First_DDL = (DropDownList)sender;
GridViewRow row = (GridViewRow)First_DDL.NamingContainer;
Response.Write(First_DDL.SelectedItem.ToString().Substring(0, 3) + "<br /><br />");
sql3 = " SELECT FROM `tbl_m` WHERE fiedl1 = ?; ";
Response.Write(sql3);
DataTable dtSubCategories = new DataTable();
using (OdbcConnection myConnectionString =
new OdbcConnection(ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString))
{
myConnectionString.Open();
using (OdbcCommand cmd = new OdbcCommand(sql3, myConnectionString))
{
OdbcDataAdapter adapter = new OdbcDataAdapter(cmd);
cmd.Parameters.AddWithValue("param1", First_DDL.SelectedItem.ToString().Substring(0, 3));
adapter.Fill(dtSubCategories);
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList Second_DDL = (DropDownList)e.Row.FindControl("Second_DDL");
Second_DDL.DataTextField = "text";
Second_DDL.DataValueField = "text";
Second_DDL.DataSource = dtSubCategories();
Second_DDL.DataBind();
}
}
编辑#2
using System;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Globalization;
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));
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();
}
}
}
答案 0 :(得分:0)
全局声明您的数据表。在First_DDL_SelectedIndexChanged()函数之外。就像这个
DataTable dtSubCategories = new DataTable();
protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{..}
您的问题是数据表只是第一个下拉列表的本地 列表。
答案 1 :(得分:0)
根据Yogi的回答和Rex的评论,当你是一个变量时,你也会像dtSubCategories
一样调用方法。删除括号或创建方法/属性以检索数据表。
//using the global variable
Second_DDL.DataSource = dtSubCategories;
//using a property
Second_DDL.DataSource = DTSubCategories;
public DataTable DTSubCategories
{
get { return dtSubCategories; }
set { dtSubCategories = value; }
}
//using a method
Second_DDL.DataSource = GetSubCategories();
private DataTable GetSubCategories()
{
return dtSubCategories;
}