|
Dear Friend,
Is anybody guide me how to show previously selected item in a listbox ?
I had used listbox control with multiple selection mode. I stored id with comma delimiter in database.
e.g.
1. List Item One.
2. List Item Two.
3. List Item Three.
4. List Item Four.
5. List Item Five.
Suppose user select item 2, 4 & 5. So I will store it as a 2,4,5 in database. This will work fine.
Now in editing I have to shows selection-bar for item 2, item 4 & item 5. So I had typed below source.
LstBoxPropHandling.SelectionMode = ListSelectionMode.Multiple;
for (j=0; j < PropSplit.Length; j++)
{
LstBoxPropHandling.SelectedValue = PropSplit[j].ToString();
}
But with this statement I got only last item id as a selected item with selectionbar. In our example 2,4,5 only item whose
id is 5 got as a selected item.
Is anybody guide me how to make item 2, item 4, item 5 as a selected item ?
Thanking You,
|
|
|
You need to basically check for the corresponding indexes matching for that value and set it selected.
Something like this..
loop through and get the index for the matching value and then set it..
LstBoxPropHandling.Items(j).Selected = True
<Added>
You need to basically check for the corresponding indexes matching for that value and set it selected.
Something like this..
loop through and get the index for the matching value and then set it..
LstBoxPropHandling.Items(i).Selected = True
|
|
|
Thanks Vim,
What you said is right but i am confused in for(foreach) loop - how to use for loop bcoz i dont know how many checkboxes and controls are there.
So please write a brief code with the for loop which can make me understand the process of getting the values of selected checkboxes - I am new bee to C#, ASP.net so ple help me......
Waiting for your reply..... pls....
Thanks,
Paresh
|
|
|
Try this..
(Assuming that you have two list boxes. One in which the user is clicking to select(PropSplit) and the other one(LstBoxPropHandling) where you are adding the selected values.. )
Dim i As Integer
For i=0 To PropSplit.Items.Count - 1
If PropSplit.Items(i).Selected Then
LstBoxPropHandling.Items.Add(PropSplit.Items(i).Value)
End If
Next
Let me know if this works..
|
|
|
Thanks for taking interest in my problem!!. But my problem is little bit different form what you understood. Below is my source code. Actually my problem is little bit different so I can't use CheckBoxList. In my case I have to assign HTML INPUT tag to label control of ASP.Net.
1. I have two table say header_master & section_master.
2. Header master will just shows Header Section Name only.
3. Section Master will display section name which follow under particular header_master on the basis of header_id.
4. Section Master also have one field rights which has generally 4 value A,E,D,S or sometime P.
5. Now I had used ExecuteReader & run loop. First I will store Header_Name in a label control, then Section_Name with <UL>
for bulleting & indentention.
6. With Section_Name I will check rights field & generate Checkbox, pl look below.
[ ] Add, [ ] Edit, [ ] Delete
[ ] Search, [ ] Approve.
I had seperate this in 2 line, because generally we have 3 normal transaction on a section says Add, Edit, Delete.
7. Checkboxes are created with HTML input tag. Algorithm isa below.
split = rights("A,E,D,S,P");
if (split == "A")
{
<INPUT TYPE="checkbox" id="chk" + Module_Id Name="chk" + Module_Id>Add;
}
if (split == "E")
{
<INPUT TYPE="checkbox" id="chk" + Module_Id Name="chk" + Module_Id>Edit;
}
if (split == "D")
{
<INPUT TYPE="checkbox" id="chk" + Module_Id Name="chk" + Module_Id>Delete;
}
if (split == "S")
{
<INPUT TYPE="checkbox" id="chk" + Module_Id Name="chk" + Module_Id>Search;
}
if (split == "P")
{
<INPUT TYPE="checkbox" id="chk" + Module_Id Name="chk" + Module_Id>Approach;
}
8. All this INPUT tag store in a label control of ASP.Net. Upto this everything is running fine.
9. My problem start from here. After pressing SUBMIT button how shall I get value of each these CheckBox with a loop ?
10. I got value of by typing Request.QueryString["chk121"]; will print True if checked.
11. But how to know that how many CheckBox control generated on a form. Also I want this with a simple for loop.
12. Below is my actual source code. (I am using Visual DOT studio for coding).
protected void ddUserList_SelectedIndexChange(object sender, System.EventArgs e)
{
btnSubmit.Enabled = true;
string parentSql;
parentSql = "SELECT Module_Id FROM Site_Module WHERE parent_id = '-1'";
rightCon = new SqlConnection(ConfigurationSettings.AppSettings["connstr"].ToString());
rightCon.Open();
SqlCommand cmdParent = new SqlCommand(parentSql, rightCon);
SqlDataReader daParent = cmdParent.ExecuteReader();
if (daParent.Read())
{
string SectionSql;
int intSectionId = 0;
intSectionId = Convert.ToInt32(daParent["Module_Id"]);
SectionSql = "SELECT Module_Id, Module_Name, Trans_Flag FROM Site_Module WHERE parent_id = " + intSectionId;
SectionCon = new SqlConnection(ConfigurationSettings.AppSettings["connstr"].ToString());
SectionCon.Open();
SqlCommand cmdSection = new SqlCommand(SectionSql, SectionCon);
SqlDataReader daSection = cmdSection.ExecuteReader();
while (daSection.Read())
{
string strDetailSql = "";
int i = 0;
int intDetailId = 0;
intDetailId = Convert.ToInt32(daSection["Module_Id"]);
lblSection.Text += "<ul><li>" + daSection["Module_Name"];
strDetailSql = "SELECT Module_Id, Module_Name, Trans_Flag FROM Site_Module WHERE parent_id = " + intDetailId;
DetailCon = new SqlConnection(ConfigurationSettings.AppSettings["connstr"].ToString());
DetailCon.Open();
SqlCommand cmdDetail = new SqlCommand(strDetailSql, DetailCon);
SqlDataReader daDetail = cmdDetail.ExecuteReader();
while (daDetail.Read())
{
lblSection.Text += "<ul><li>" + daDetail["Module_Name"] + "<br>";
string[]split = daDetail["Trans_Flag"].ToString().Split(',');
for (i = 0; i < split.Length; i++)
{
switch (split)
{
case "A" :
{
lblSection.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='A'>Add ";
//cbList.Items.Add(new ListItem(Convert.ToString(daDetail["Module_id"]), "Add"));
break;
}
case "E" :
{
lblSection.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='E'>Edit ";
//cbList.Items.Add(new ListItem(Convert.ToString(daDetail["Module_id"]), "Edit"));
break;
}
case "D" :
{
lblSection.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='D'>Delete ";
//cbList.Items.Add(new ListItem(Convert.ToString(daDetail["Module_id"]), "Delete"));
break;
}
case "S" :
{
lblSection.Text += "<input type='checkbox' name='chk[" + daDetail["Module_id"] + i.ToString() + "]' value='S'>Search ";
//cbList.Items.Add(new ListItem(Convert.ToString(daDetail["Module_id"]), "Search"));
break;
}
} // End of switch (split)
} // End of for (i = 0; i < split.Length; i++)
lblSection.Text += "<br></li></ul>";
} // End of while (daDetail.Read())
daDetail.Close();
cmdDetail.Connection.Close();
DetailCon.Close();
lblSection.Text += "</li></ul>";
}
daSection.Close();
cmdSection.Connection.Close();
SectionCon.Close();
}
daParent.Close();
cmdParent.Connection.Close();
rightCon.Close();
}
Waiting for your reply......
Thanks,
Paresh
|
|
|
|
|
|
|
|