asp.net下拉列表和复选框列表 [英] asp.net dropdownlist and checkboxlist

查看:105
本文介绍了asp.net下拉列表和复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好,

我的.aspx代码在下面给出...

good afternoon to all,

my .aspx code given below...

<table class="style1">
        <tr>
            <td>
                <asp:DropDownList ID="Ddl" runat="server"

                    onselectedindexchanged="Ddl_SelectedIndexChanged">
                    <asp:ListItem Value=0 Selected="True">------Select-----</asp:ListItem>
                    <asp:ListItem Value=1>National Sales Manager</asp:ListItem>
                    <asp:ListItem Value=2>Regional Manager</asp:ListItem>
                </asp:DropDownList>
            </td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" Height="66px" Width="689px">
                </asp:CheckBoxList>
            </td>
            <td>
                &nbsp;</td>
        </tr>
    </table>




-----和我的.aspx.cs ----------




-----and my .aspx.cs----------

public void DataLode(string Designation)
    {
        string connectionString = (string)ConfigurationSettings.AppSettings["ConnectionString"];
        ConnDeemah.ConnectionString = connectionString;
        CheckBoxList1.Visible = true;
       
        
        if (Designation == "1")
        {
            ConnDeemah.Open();
            SqlDataReader nsmDr;
            Cmd = "SELECT Convert(varchar, NSM.NationalSalesManagerCode)+ '' - '' + NSM.NationalSalesManagerName + '' / '' + C.CountryName As NationalSalesManagerName, NSM.NationalSalesManagerCode FROM NationalSalesManager NSM INNER JOIN Country C ON NSM.NationalSalesManagerCode = C.NationalSalesManagerCode ORDER BY NationalSalesManagerName";
            SqlCommand RCMD = new SqlCommand(Cmd, ConnDeemah);
            nsmDr = RCMD.ExecuteReader(CommandBehavior.CloseConnection);
            CheckBoxList1.DataSource = nsmDr;
            CheckBoxList1.DataTextField = "NationalSalesManagerName";
            CheckBoxList1.DataValueField = "NationalSalesManagerCode";
            CheckBoxList1.DataBind();
        }
        else if (Designation == "2")
        {
            ConnDeemah.Open();
            SqlDataReader nsmDr;
            Cmd = "SELECT Convert(varchar, RMgr.RegionManagerCode)+ '' - '' + RMgr.RegionManagerName + '' / '' + RMst.RegionMstName As RegionManagerName, RMgr.RegionManagerCode FROM RegionManager RMgr INNER JOIN RegionMaster RMst  ON RMgr.RegionManagerCode = RMst.RegionManagerCode ORDER BY RMgr.RegionManagerCode";
            SqlCommand RCMD = new SqlCommand(Cmd, ConnDeemah);
            nsmDr = RCMD.ExecuteReader(CommandBehavior.CloseConnection);
            CheckBoxList1.DataSource = nsmDr;
            CheckBoxList1.DataTextField = "RegionManagerName";
            CheckBoxList1.DataValueField = "RegionManagerCode";
            CheckBoxList1.DataBind();
 
        }
    }
   
    protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
       if (Ddl.SelectedValue=="1")
        {
            CheckBoxList1.Visible = true;
            DataLode(Ddl.SelectedItem.Value);
        }
       else if (Ddl.SelectedValue == "2")
       {
           DataLode(Ddl.SelectedItem.Value);
       }
      }

 }



当我运行页面时,它不会填充checkboxlist数据,但是当我放置下面给出的代码page_load事件时,它将显示checkboxlist数据.



when i run my page its not populate the checkboxlist data but when i put the code page_load event given below then its show the checkboxlist data.

ConnDeemah.Open();
SqlDataReader nsmDr;
Cmd = "SELECT Convert(varchar, NSM.NationalSalesManagerCode)+ '' - '' + NSM.NationalSalesManagerName + '' / '' + C.CountryName As NationalSalesManagerName, NSM.NationalSalesManagerCode FROM NationalSalesManager NSM INNER JOIN Country C ON NSM.NationalSalesManagerCode = C.NationalSalesManagerCode ORDER BY NationalSalesManagerName";
SqlCommand RCMD = new SqlCommand(Cmd, ConnDeemah);
nsmDr = RCMD.ExecuteReader(CommandBehavior.CloseConnection);
CheckBoxList1.DataSource = nsmDr;
CheckBoxList1.DataTextField = "NationalSalesManagerName";
CheckBoxList1.DataValueField = "NationalSalesManagerCode";
CheckBoxList1.DataBind();




因此,请帮助我选择下拉项时如何填充复选框列表数据.




so please help me how i can populate the checkboxlist data on selecting dropdown item.

推荐答案

嗨 瓦夸斯,

请按以下方式进行更改:
Hi Vaquas,

Please make changes as bellow:
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       BindCheckboxList();
    }
}
private void BindCheckboxList()
{
  ConnDeemah.Open();
  SqlDataReader nsmDr;
  Cmd = "SELECT Convert(varchar, NSM.NationalSalesManagerCode)+ ' - ' + NSM.NationalSalesManagerName + ' / ' + C.CountryName As NationalSalesManagerName, NSM.NationalSalesManagerCode FROM NationalSalesManager NSM INNER JOIN Country C ON NSM.NationalSalesManagerCode = C.NationalSalesManagerCode ORDER BY NationalSalesManagerName";
SqlCommand RCMD = new SqlCommand(Cmd, ConnDeemah);
nsmDr = RCMD.ExecuteReader(CommandBehavior.CloseConnection);
CheckBoxList1.DataSource = nsmDr;
CheckBoxList1.DataTextField = "NationalSalesManagerName";
CheckBoxList1.DataValueField = "NationalSalesManagerCode";
CheckBoxList1.DataBind();
}
protected void Ddl_SelectedIndexChanged(object sender, EventArgs e)
{
   if (Ddl.SelectedValue=="1")
   {
       CheckBoxList1.Visible = true;
   }
   BindCheckboxList();
}




如有任何疑问,请让我知道.

如果有帮助,请提供"投票",如果这是正确的答案,请提供"接受答案".:rose:

谢谢,
Imdadhusen




Please do let me know, if you have any doubt.

Please provide "Vote" if this would be helpful, and make "Accept Answer" if this would be correct answer.:rose:

Thanks,
Imdadhusen


添加dropdownlist的属性 AutoPostBack ="True"

Add property AutoPostBack="True" of dropdownlist

<asp:DropDownList ID="Ddl" AutoPostBack="True" runat="server"

                    onselectedindexchanged="Ddl_SelectedIndexChanged">
                    <asp:ListItem Value=0 Selected="True">------Select-----</asp:ListItem>
                    <asp:ListItem Value=1>National Sales Manager</asp:ListItem>
                    <asp:ListItem Value=2>Regional Manager</asp:ListItem>
                </asp:DropDownList>


亲爱的Imad,
感谢您的回答

我不想在page_load事件中填充复选框列表,我需要在从下拉列表中选择项目时根据其他条件填充复选框列表.
dear Imad,
thanks for ur answer

i dont want to populate the checkboxlist on page_load event ,i need when i select the item from drop down list the its populate the checkboxlist according to if else condition.


这篇关于asp.net下拉列表和复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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