簡體   English   中英

C#中的頁腳模板DropDownlist

[英]Footer Template DropDownlist in c#

我需要cascading DropDownList in ASP.Net c#創建cascading DropDownList in ASP.Net c#

第二個DropDownList中的值取決於在第一個DropDownList中選擇的值。

我需要在GridView.Footer TemplateGridView.

當從第一個DropDownList中選擇一個值時,我需要使用在First_DDL_SelectedIndexChanged事件中執行的查詢sql3的輸出填充第二個DropDownList。

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM