Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
good afternoon to all,

my .aspx code given below...

XML
<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>



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

 }


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.
Posted
Updated 27-Oct-10 23:45pm
v2
Comments
Sunasara Imdadhusen 28-Oct-10 5:44am    
Please use PRE tag to format your code!
Sunasara Imdadhusen 28-Oct-10 5:56am    
Please try to write optimized code instead of repetitive lines.

Hi Vaquas,

Please make changes as bellow:
C#
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();
}



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
 
Share this answer
 
Add property AutoPostBack="True" of dropdownlist

XML
<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>
 
Share this answer
 
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.
 
Share this answer
 
Comments
Hiren solanki 28-Oct-10 6:46am    
complements and further query can be put as a comment inside answer. don't consume another answer having full 10 point. Please
raju melveetilpurayil 28-Oct-10 7:39am    
yas hiran is correct. dont hit answer tab for add comment.
Hiren solanki 28-Oct-10 8:31am    
little spelling mistake raju, it's 'hiren' :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900