Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
how can i display an checkboxlist selected value under edit mode


I have tried, but it was showing only the first field selected..
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {           
            if (Request.QueryString["Id"] != null)
            {
                Edit();                
            }
        }
    }
 
    protected void Edit()
    {
        int id = Convert.ToInt32(Request.QueryString["Id"].ToString());
        DataTable dt1 = s.GetAccessRightsByRole(id.ToString());           
            if (dt1.Rows.Count > 0)
            {         
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (dt1.Rows[i]["RightId"].ToString() == "1")
                    {
                        CheckBoxList1.Items[0].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "2")
                    {
                        CheckBoxList1.Items[1].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "3")
                    {
                        CheckBoxList1.Items[2].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "4")
                    {
                        CheckBoxList1.Items[3].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "5")
                    {
                        CheckBoxList1.Items[4].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "6")
                    {
                        CheckBoxList1.Items[5].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "7")
                    {
                        CheckBoxList1.Items[6].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "8")
                    {
                        CheckBoxList1.Items[7].Selected = true;
                    }
                    if (dt1.Rows[i]["RightId"].ToString() == "9")
                    {
                        CheckBoxList1.Items[8].Selected = true;
                    }
                }            
            
            if (dt1.Rows.Count == 0)
            {
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = false;
                }
            }
        }
    }

help
Posted
v2
Comments
Not clear about the requirement.
What exactly you want to do ?
sahmed4 26-Mar-13 2:51am    
dash,
Please have a look in to my code i am trying to edit the fields which also has a check box list control where i have binded values dynamically..
<pre lang="cs">protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["Id"] != null)
{
Edit();
}
}
}

protected void Edit()
{
int id = Convert.ToInt32(Request.QueryString["Id"].ToString());
DataTable dt1 = s.GetAccessRightsByRole(id.ToString());
if (dt1.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (dt1.Rows[i]["RightId"].ToString() == "1")
{
CheckBoxList1.Items[0].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "2")
{
CheckBoxList1.Items[1].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "3")
{
CheckBoxList1.Items[2].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "4")
{
CheckBoxList1.Items[3].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "5")
{
CheckBoxList1.Items[4].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "6")
{
CheckBoxList1.Items[5].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "7")
{
CheckBoxList1.Items[6].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "8")
{
CheckBoxList1.Items[7].Selected = true;
}
if (dt1.Rows[i]["RightId"].ToString() == "9")
{
CheckBoxList1.Items[8].Selected = true;
}
}

if (dt1.Rows.Count == 0)
{
for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
CheckBoxList1.Items[i].Selected = false;
}
}
}
}</pre>
You have written like

for (int i = 0; i < dt.Rows.Count; i++)

What is dt here ?
And have you debugged to see what is happening ?

1 solution

just change the name dt to dt1.

for (int i = 0; i < dt1.Rows.Count; i++)
 
Share this answer
 

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