Answered by:
multi-select dropdownlist with checkboxes in gridview

Question
-
User-129908252 posted
I'm hard coded the List box with some of list items
<asp:TemplateField HeaderText="PrivilegedCategoriesList" HeaderStyle-HorizontalAlign="Left"> <EditItemTemplate> <asp:ListBox ID="ddlPrivilegedCategoriesList" runat="server" SelectedValue='<%# Eval("PrivilegedCategoriesList") %>' SelectionMode="Multiple"> <asp:ListItem Value="Active" Text="Active"></asp:ListItem> <asp:ListItem Value="InActive" Text="InActive"></asp:ListItem> <asp:ListItem Value="Na" Text="NA"></asp:ListItem> </asp:ListBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblPrivilegedCategoriesList" runat="server" Text='<%# Eval("PrivilegedCategoriesList") %>'></asp:Label> </ItemTemplate> </asp:TemplateField>
I'm doing edit click and rowdatabound dataset i'm getting the value of particular on edit row like "Active,InActive" while binding the rowdatabound getting the error and its terminating. some times i'm not getting the find conlrol drop down selected value in rowdatabound.
var ddlPrivilegedCategoriesList1 = e.Row.FindControl("PrivilegedCategoriesList") as DropDownList; if (ddlPrivilegedCategoriesList != null) { for (int i = 0; i < ddlPrivilegedCategoriesList.Items.Count; i++) { string ResultString = ds.Tables[0].Rows[0]["PrivilegedCategoriesList"].ToString(); foreach (string category in ResultString.Split(',')) { if (category != ddlPrivilegedCategoriesList.Items[i].Value) continue; ddlPrivilegedCategoriesList.Items[i].Selected = true; break; } } }
Please can you tell me the solution
Thursday, August 4, 2016 6:42 AM
Answers
-
User-271186128 posted
Hi jagan12013,
<asp:ListBox ID="ddlPrivilegedCategoriesList" runat="server" SelectedValue='<%# Eval("PrivilegedCategoriesList") %>' SelectionMode="Multiple">
var ddlPrivilegedCategoriesList1 = e.Row.FindControl("PrivilegedCategoriesList") as DropDownList;
From your description, I checked your code carefully. Firstly, I find that you want to get the ListBox through above statement. But, from your code, we can see the ListBox ID is “ddlPrivilegedCategoriesList”, I suggest you could replace it.
Secondly, as we know, ddlPrivilegedCategoriesList is a ListBox. You want to convert it to DropDownList using as syntax. Because if converting unsuccessfully, as syntax will return null instead of throwing an error. So, if you set a break point in your code, you will find the control is null.
Therefore, I suggest you modify your code as below:
ListBox ddlPrivilegedCategoriesList1 = e.Row.FindControl("ddlPrivilegedCategoriesList") as ListBox;
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 5, 2016 8:44 AM -
User-1716253493 posted
Try like this to select
if(ddlPrivilegedCategoriesList.Items.FindByValue("abc")!=null) { ddlPrivilegedCategoriesList.Items.FindByValue("abc").Selected = true; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 5, 2016 8:59 AM
All replies
-
User-271186128 posted
Hi jagan12013,
<asp:ListBox ID="ddlPrivilegedCategoriesList" runat="server" SelectedValue='<%# Eval("PrivilegedCategoriesList") %>' SelectionMode="Multiple">
var ddlPrivilegedCategoriesList1 = e.Row.FindControl("PrivilegedCategoriesList") as DropDownList;
From your description, I checked your code carefully. Firstly, I find that you want to get the ListBox through above statement. But, from your code, we can see the ListBox ID is “ddlPrivilegedCategoriesList”, I suggest you could replace it.
Secondly, as we know, ddlPrivilegedCategoriesList is a ListBox. You want to convert it to DropDownList using as syntax. Because if converting unsuccessfully, as syntax will return null instead of throwing an error. So, if you set a break point in your code, you will find the control is null.
Therefore, I suggest you modify your code as below:
ListBox ddlPrivilegedCategoriesList1 = e.Row.FindControl("ddlPrivilegedCategoriesList") as ListBox;
Hope this can help you. If you have any question and confusion about the problem. Please don't hesitate to let me know.
Best regards,
Dillion- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 5, 2016 8:44 AM -
User-1716253493 posted
Try like this to select
if(ddlPrivilegedCategoriesList.Items.FindByValue("abc")!=null) { ddlPrivilegedCategoriesList.Items.FindByValue("abc").Selected = true; }
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 5, 2016 8:59 AM