Answered by:
Group options for Asp.Net DropDownList after PostBack it is not Displaying Selected Item..

Question
-
User-1030697014 posted
please go to this link
http://mpurna.blogspot.in/2012/01/group-options-for-aspnet-dropdownlist.html
after PostBack the page it selecting First ListItem Every time ..Not selected which i have selected ListItem..
how to display the selected item after postback also..
Thanks
Wednesday, August 24, 2016 1:57 PM
Answers
-
User-707554951 posted
Hi aadikumar,
1. Lost selected option after a post back.For your first question, I supposed that you bind custom DDL in every time page load rather than in the first page load. So, I suggest you could to add if (!IsPostBack) in your page load event.
2. how to get the Datagroup Name using C#For you second question, I suggest you could use Attributes Property of ListItem to get DataGrop Name
The code below is a sample you could refer to it:<cc1:DropDownList_OptGroup ID="DropDownList_OptGroup1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_OptGroup1_SelectedIndexChanged"> </cc1:DropDownList_OptGroup> <asp:Button ID="Button1" runat="server" Text="Button" />
CodeBehind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("select * from CustomTable")) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { sda.Fill(ds); DataView regionview = ds.Tables[0].DefaultView; regionview.Sort = "Regin asc"; DropDownList_OptGroup1.DataSource = regionview; DropDownList_OptGroup1.DataTextField = "Name"; DropDownList_OptGroup1.DataValueField = "Id"; DropDownList_OptGroup1.DataGroupField = "Regin"; DropDownList_OptGroup1.DataBind(); ListItem li = new ListItem("--Select--", "0"); li.Attributes.Add("DataGroupField", ""); DropDownList_OptGroup1.Items.Insert(0, li); // string s=DropDownList_OptGroup1.DataGroupField.ToString(); string s = DropDownList_OptGroup1.SelectedItem.Attributes["DataGroupField"]; } } } } } } protected void DropDownList_OptGroup1_SelectedIndexChanged(object sender, EventArgs e) { //get datagroup name string s = DropDownList_OptGroup1.SelectedItem.Attributes["DataGroupField"]; }
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
Cathy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 31, 2016 1:38 PM
All replies
-
User-707554951 posted
Hi aadikumar,
From your description, you want to group option by using ‘optgroup’ in a HTML Select Control, however, you lost selected option after a post back. If that is the case, I suggest you could use a HiddenField to save selected value. Then, resetting selected value on Button’s OnClientClick function by using jquery.
Here is an example, it works well, you could refer to it.
<script src="../../Scripts/jquery-3.1.0.min.js"></script> <script type="text/javascript"> function MyFunction() { $("#HiddenField1").val($("#DropDownList_OptGroup1").val()); } $(function () { var value = $("#HiddenField1").val(); if (value.length > 0) { $("#DropDownList_OptGroup1").val(value); } }) </script> <select id="DropDownList_OptGroup1" name="DropDownList_OptGroup1" > <option value="0" selected="selected">--Select--</option> <optgroup label="East India"> <option value="1">Bihar</option> <option value="2">West Bengal</option> </optgroup> <optgroup label="North East India"> <option value="3">Assam</option> <option value="4">Arunachal Pradesh</option> </optgroup> </select> </div> <asp:HiddenField ID="HiddenField1" runat="server" Value="" /> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" OnClientClick="MyFunction()" Text="Button" />
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
CathyThursday, August 25, 2016 7:27 AM -
User-1030697014 posted
Hi,
Thanks for your Reply and this code helps me lot.
but i am looking for server side Code please go through this link only Server Side code...
http://mpurna.blogspot.in/2012/01/group-options-for-aspnet-dropdownlist.html
1. Lost selected option after a post back.
2. how to get the Datagroup Name using C#
or is there any other control with the same functionality.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
namespace DropDownList_OptGroup
{
///
/// Summary description for DropDownList_OptGroup.
///
[ToolboxData("<{0}:DropDownList_OptGroup runat=server>")]
public class DropDownList_OptGroup : System.Web.UI.WebControls.DropDownList
{
///
/// The field in the datasource which provides values for groups
///
[DefaultValue(""), Category("Data")]
public virtual string DataGroupField
{
get
{
object obj = this.ViewState["DataGroupField"];
if (obj != null)
{
return (string)obj;
}
return string.Empty;
}
set
{
this.ViewState["DataGroupField"] = value;
}
}
///
/// if a group doesn't has any enabled items,there is no need
/// to render the group too
///
/// ///
private bool IsGroupHasEnabledItems(string groupName)
{
ListItemCollection items = this.Items;
ListItem item;
for (int i = 0; i < items.Count; i++)
{
item = items[i];
if (item.Attributes["DataGroupField"].Equals(groupName) && item.Enabled)
{
return true;
}
}
return false;
}
///
/// Render this control to the output parameter specified.
/// Based on the source code of the original DropDownList method
///
/// The HTML writer to write out to
protected override void RenderContents(HtmlTextWriter writer)
{
ListItemCollection items = this.Items;
int itemCount = this.Items.Count;
string curGroup = String.Empty;
string itemGroup;
bool bSelected = false;
if (itemCount <= 0)
{
return;
}
for (int i = 0; i < itemCount; i++)
{
ListItem item = items[i];
itemGroup = (string)item.Attributes["DataGroupField"];
if (itemGroup != null && itemGroup != curGroup && IsGroupHasEnabledItems(itemGroup))
{
if (curGroup != String.Empty)
{
writer.WriteEndTag("optgroup");
writer.WriteLine();
}
curGroup = itemGroup;
writer.WriteBeginTag("optgroup");
writer.WriteAttribute("label", curGroup, true);
writer.Write('>');
writer.WriteLine();
}
// we don't want to render disabled items
if (item.Enabled)
{
writer.WriteBeginTag("option");
if (item.Selected)
{
if (bSelected)
{
throw new HttpException("Cant_Multiselect_In_DropDownList");
}
bSelected = true;
writer.WriteAttribute("selected", "selected", false);
}
writer.WriteAttribute("value", item.Value, true);
writer.Write('>');
HttpUtility.HtmlEncode(item.Text, writer);
writer.WriteEndTag("option");
writer.WriteLine();
}
}
if (curGroup != String.Empty)
{
writer.WriteEndTag("optgroup");
writer.WriteLine();
}
}
///
/// Perform data binding logic that is associated with the control
///
/// An EventArgs object that contains the event data
protected override void OnDataBinding(EventArgs e)
{
// Call base method to bind data
base.OnDataBinding(e);
if (this.DataGroupField == String.Empty)
{
return;
}
// For each Item add the attribute "DataGroupField" with value from the datasource
IEnumerable dataSource = GetResolvedDataSource(this.DataSource, this.DataMember);
if (dataSource != null)
{
ListItemCollection items = this.Items;
int i = 0;
string groupField = this.DataGroupField;
foreach (object obj in dataSource)
{
string groupFieldValue = DataBinder.GetPropertyValue(obj, groupField, null);
ListItem item = items[i];
item.Attributes.Add("DataGroupField", groupFieldValue);
i++;
}
}
}
///
/// This is copy of the internal ListControl method
///
/// /// ///
private IEnumerable GetResolvedDataSource(object dataSource, string dataMember)
{
if (dataSource != null)
{
IListSource source1 = dataSource as IListSource;
if (source1 != null)
{
IList list1 = source1.GetList();
if (!source1.ContainsListCollection)
{
return list1;
}
if ((list1 != null) && (list1 is ITypedList))
{
ITypedList list2 = (ITypedList)list1;
PropertyDescriptorCollection collection1 = list2.GetItemProperties(new PropertyDescriptor[0]);
if ((collection1 == null) || (collection1.Count == 0))
{
throw new HttpException("ListSource_Without_DataMembers");
}
PropertyDescriptor descriptor1 = null;
if ((dataMember == null) || (dataMember.Length == 0))
{
descriptor1 = collection1[0];
}
else
{
descriptor1 = collection1.Find(dataMember, true);
}
if (descriptor1 != null)
{
object obj1 = list1[0];
object obj2 = descriptor1.GetValue(obj1);
if ((obj2 != null) && (obj2 is IEnumerable))
{
return (IEnumerable)obj2;
}
}
throw new HttpException("ListSource_Missing_DataMember");
}
}
if (dataSource is IEnumerable)
{
return (IEnumerable)dataSource;
}
}
return null;
}
#region Internal behaviour
/// Saves the state of the view.
///
protected override object SaveViewState()
{
// Create an object array with one element for the CheckBoxList's'
// ViewState contents, and one element for each ListItem in skmCheckBoxList
object[] state = new object[this.Items.Count + 1];
object baseState = base.SaveViewState();
state[0] = baseState;
// Now, see if we even need to save the view state
bool itemHasAttributes = false;
for (int i = 0; i < this.Items.Count; i++)
{
if (this.Items[i].Attributes.Count > 0)
{
itemHasAttributes = true;
// Create an array of the item's Attribute's keys and values
object[] attribKV = new object[this.Items[i].Attributes.Count * 2];
int k = 0;
foreach (string key in this.Items[i].Attributes.Keys)
{
attribKV[k++] = key;
attribKV[k++] = this.Items[i].Attributes[key];
}
state[i + 1] = attribKV;
}
}
// return either baseState or state, depending on whether or not
// any ListItems had attributes
if (itemHasAttributes)
return state;
else
return baseState;
}
///
/// Loads the state of the view.
///
/// State of the saved.
protected override void LoadViewState(object savedState)
{
if (savedState == null) return;
// see if savedState is an object or object array
if (savedState is object[])
{
// we have an array of items with attributes
object[] state = (object[])savedState;
base.LoadViewState(state[0]); // load the base state
for (int i = 1; i < state.Length; i++)
{
if (state[i] != null)
{
// Load back in the attributes
object[] attribKV = (object[])state[i];
for (int k = 0; k < attribKV.Length; k += 2)
this.Items[i - 1].Attributes.Add(attribKV[k].ToString(),
attribKV[k + 1].ToString());
}
}
}
else
// we have just the base state
base.LoadViewState(savedState);
}
#endregion
}
}
5) Rebuild the solution
6) Open the solution of your website
7) Toolbox-> Right Click on it and select "Add tab", then write the name of the tab as Extended Controls
8) Right-click on that tab then select "choose items",then from ".Net FrameWork Components" click "Browse",
then point to the dll that was created by our class library..it's name is "DropDownList_OptGroup.dll".then click OK,then OK for the other dialog, you will notice that our dropdownlist control has been added to the toolbox
9) drag-n-drop DropDownList_OptGroup control
Usage:Countryid Country_name Region
---------- ------------------ ------------------
1 Bihar East India
2 West Bengal East India
3 Assam North East India
4 Arunachal Pradesh North East India
5 Rajasthan North India
6 Punjab North India
7 Jammu and Kashmir North India
8 Andhra Pradesh South India
9 Tamilnadu South India
10 Karnataka South India
11 Kerala South India
12 Gujarat West India
13 Maharashtra West India
DataView regionview = ds.Tables[0].DefaultView;
regionview.Sort = "region asc";
DropDownList_OptGroup1.DataSource = regionview;
DropDownList_OptGroup1.DataTextField = "country_name";
DropDownList_OptGroup1.DataValueField = "countryid";
DropDownList_OptGroup1.DataGroupField = "region";
DropDownList_OptGroup1.DataBind();
ListItem li = new ListItem("--Select--", "0");
li.Attributes.Add("DataGroupField", "");
DropDownList_OptGroup1.Items.Insert(0, li);
Thanks.
Thursday, August 25, 2016 1:34 PM -
User-707554951 posted
Hi aadikumar,
1. Lost selected option after a post back.For your first question, I supposed that you bind custom DDL in every time page load rather than in the first page load. So, I suggest you could to add if (!IsPostBack) in your page load event.
2. how to get the Datagroup Name using C#For you second question, I suggest you could use Attributes Property of ListItem to get DataGrop Name
The code below is a sample you could refer to it:<cc1:DropDownList_OptGroup ID="DropDownList_OptGroup1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList_OptGroup1_SelectedIndexChanged"> </cc1:DropDownList_OptGroup> <asp:Button ID="Button1" runat="server" Text="Button" />
CodeBehind:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("select * from CustomTable")) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { sda.Fill(ds); DataView regionview = ds.Tables[0].DefaultView; regionview.Sort = "Regin asc"; DropDownList_OptGroup1.DataSource = regionview; DropDownList_OptGroup1.DataTextField = "Name"; DropDownList_OptGroup1.DataValueField = "Id"; DropDownList_OptGroup1.DataGroupField = "Regin"; DropDownList_OptGroup1.DataBind(); ListItem li = new ListItem("--Select--", "0"); li.Attributes.Add("DataGroupField", ""); DropDownList_OptGroup1.Items.Insert(0, li); // string s=DropDownList_OptGroup1.DataGroupField.ToString(); string s = DropDownList_OptGroup1.SelectedItem.Attributes["DataGroupField"]; } } } } } } protected void DropDownList_OptGroup1_SelectedIndexChanged(object sender, EventArgs e) { //get datagroup name string s = DropDownList_OptGroup1.SelectedItem.Attributes["DataGroupField"]; }
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
Cathy- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 31, 2016 1:38 PM