首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >级联DropDownList在ASP.Net c#中的应用

级联DropDownList在ASP.Net c#中的应用
EN

Stack Overflow用户
提问于 2014-08-29 08:17:23
回答 2查看 674关注 0票数 0

我需要在cascading DropDownList in ASP.Net c#中创建一个DropDownList,它依赖于GridView.中的Footer Template中的数据的第一个DropDownList

当从第一个First_DDL_SelectedIndexChanged**.**中选择一个值时,我需要使用查询sql3的值填充第二个DropDownList

在调试中,在第一个DropDownList中选择的输出值和查询sql3是正确的。

我尝试过使用此解决方案,但没有成功,因为错误是:

代码语言:javascript
复制
Compiler Error Message: CS0103: 
The name 'dtSubCategories' does not exist in the current context

在这一行代码背后:

代码语言:javascript
复制
Line 360:
Second_DDL.DataSource = dtSubCategories();

如果你能帮我解决这个问题,我将不胜感激。

这是我的代码:

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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();
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2014-08-29 09:19:24

全局声明数据表。外部First_DDL_SelectedIndexChanged() function.Like

代码语言:javascript
复制
DataTable dtSubCategories = new DataTable();
protected void First_DDL_SelectedIndexChanged(object sender, EventArgs e)
{..}

您的问题是datatable是本地的,只有第一个下拉列表。

票数 0
EN

Stack Overflow用户

发布于 2014-08-29 12:52:05

在扩展Yogi的答案和Rex的注释之后,当dtSubCategories是一个变量时,您也会像调用方法一样调用它。要么删除括号,要么创建方法/属性来检索数据表。

代码语言:javascript
复制
//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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25564283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档