首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >页脚模板DropDownlist中的c#

页脚模板DropDownlist中的c#
EN

Stack Overflow用户
提问于 2014-08-31 10:57:44
回答 1查看 1K关注 0票数 1

我需要创建一个cascading DropDownList in ASP.Net c#.

第二个DropDownList中的值取决于在第一个DropDownList中选择的值。

我需要把这个放在我GridView.GridView.

当从第一个First_DDL_SelectedIndexChanged事件中选择一个值时,我需要用在DropDownList事件中执行的查询sql3的输出填充第二个DropDownList。

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

我尝试过使用这个解决方案但没有成功,因为我没有错误,但是即使在选择Second_DDL中的值时,First_DDL.仍然是空的

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

这是我的代码:

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

回答 1

Stack Overflow用户

发布于 2014-09-01 06:42:46

试试这个:

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

在你的代码背后:

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

https://stackoverflow.com/questions/25591294

复制
相关文章

相似问题

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