将级联DropDown列表绑定到标签 [英] Bind Cascading DropDown List to the Label

查看:134
本文介绍了将级联DropDown列表绑定到标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个级联下拉列表(组和名称)并为组下拉列表启用autopostback true ...但是当我为名称下拉列表启用autopostback为true时...它不允许我在名称下拉列表中选择任何其他值...它直接选择第一个值并显示第一个员工的名称...而当我禁用名称下拉列表的自动回复时,它不会触发标签的任何值...我在哪里错了?任何人都可以帮助我。下面是C#代码





I have two cascading dropdownist(group and name) and enabled the autopostback true for the group dropdownlist...but when i enable the autopostback true for name dropdownlist...it doesnt let me select any other values in the name dropdownlist...it directly selects the first value and displays the first employee's designation...whereas when I disable the autopostback for the name dropdownlist it doesnt trigger any value for the label...where am i goin wrong?can anyone help me out. Below is the C# code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.Configuration;


public partial class Forms_Master_Forms_Temporary_duty_form : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Server=SOFTDEV1; Database=ADE_2903_SOFTDEV; Integrated security = true");
    protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList7.Visible = false;

        if (!IsPostBack)
        {
            Getdata();//adding the division to the dropdown box
        }
    }

    /* Adding The contents to the Division DropDown List */
    
    private void Getdata()
    {
        SqlCommand cmd = new SqlCommand("SELECT DivisionID, DivShort FROM M_Division ORDER BY DivShort", conn);
        DataSet objDs = new DataSet();
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        conn.Open();
        sd.Fill(objDs);
        conn.Close();
        if (objDs.Tables[0].Rows.Count > 0)
         {
            ddlgroup.DataSource = objDs.Tables[0];
            ddlgroup.DataTextField = "DivShort";
            ddlgroup.DataValueField = "DivisionID";
            ddlgroup.DataBind();
            ddlgroup.Items.Insert(0, "--Select--");
         }
    }

    /*Adding the names to the Name DropDownList based on the selected division from Division Drop Down List*/

    private void FillName(int divisionID)
    {
        SqlCommand cmd = new SqlCommand("SELECT DivisionID, EmpName FROM M_Emp_Personal WHERE DivisionID =@DivisionID AND IsActive=1   ORDER BY EmpName", conn);
        cmd.Parameters.AddWithValue("@DivisionID", divisionID);
        DataSet objDs = new DataSet();
        SqlDataAdapter sd = new SqlDataAdapter(cmd);
        conn.Open();
        sd.Fill(objDs);
        conn.Close();
        if (objDs.Tables[0].Rows.Count > 0)
        {
            ddlname.DataSource = objDs.Tables[0];
            ddlname.DataTextField = "EmpName";
            ddlname.DataValueField = "DivisionID";
            ddlname.DataBind();
            ddlname.Items.Insert(0, "--Select--");
        }
    }

    /*Selecting the division present in the dropdownlist*/

    protected void ddlgroup_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int DivisionID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
            FillName(DivisionID);
            ddlname.SelectedIndex = 0;
     }

    /*Selecting the name present in the dropdownlist*/
    protected void ddlname_SelectedIndexChanged(object sender, EventArgs e)
    {
        int DesingID = Convert.ToInt32(ddlgroup.SelectedValue.ToString());
        FillDesig(DesingID);

    }

    private void FillDesig(int desingID)
    {
        SqlCommand cmd = new SqlCommand("Select M_Designation.DesigLong,M_Emp_Personal.EmpName From M_Designation inner join M_Emp_Personal on M_Designation.DesigID=M_Emp_Personal.DesigID", conn);
        cmd.Parameters.AddWithValue("@DesigID", desingID);
       
        conn.Open();
       
        lbldes.Text = cmd.ExecuteScalar().ToString();
        conn.Close();
    }

}

The ASP.NET code is

<pre lang="HTML"> <asp:DropDownList ID="ddlgroup" runat="server" 
                 
                
                style="z-index: 1; left: 118px; top: 148px; position: absolute; height: -5px;" 
                AutoPostBack="True" 
                onselectedindexchanged="ddlgroup_SelectedIndexChanged1">
            
            </asp:DropDownList>
<asp:DropDownList ID="ddlname" runat="server" 
               
                style="z-index: 1; left: 332px; top: 147px; position: absolute; height: 35px; width: 117px;" 
                onselectedindexchanged="ddlname_SelectedIndexChanged" AutoPostBack="True">
            </asp:DropDownList>

  <table style="width: 85%; removed 207px; removed 14px; removed: absolute; height: 593px;">
                <tr>
                    <td class="style5">
                        2.</td>
                    <td class="style7">
                        DESIGNATION</td>
                    <td colspan="3">
                        <asp:Label ID="lbldes" runat="server" Text="Label"></asp:Label>
                    </td>
                </tr>

推荐答案





您能否发布您的设计代码?所以我可以深入研究它
Hi,

Can you please post your design code ? So I can dig more into it


在事件中:ddlgroup_SelectedIndexChanged1,



remove ddlname.SelectedIndex = 0;



别忘了给解决方案评分并将其标记为答案。



问候,

CodeBlack
In the event : ddlgroup_SelectedIndexChanged1,

remove ddlname.SelectedIndex = 0;

Dont forget to rate the solution and mark it as an answer.

Regards,
CodeBlack


这篇关于将级联DropDown列表绑定到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆