for (int i = 0; i < mydropdown.Items.Count; i++)
{
if (condition)
{
mydropdown.Items[i].Attributes.Add("style", "background-color:yellow");
mydropdown.Items[i].Attributes.Add("Title", "some text");
}
else
{
mydropdown.Items[i].Attributes.Add("style", "background-color:white");
mydropdown.Items[i].Attributes.Add("Title", "some other text here");
}
}
i lost this background color, tooltip etc when page postback
Problem has been solved by customizing dropdownlist as:
Steps:
1. create library
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
ToolboxBitmap(typeof(DropDownList)),
ToolboxData("<{0}:NewDropDownList runat=server></{0}:NewDropDownList>")]
public class NewDropDownList : DropDownList
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
// the +1 is to hold the base info
object baseState = base.SaveViewState();
allStates[0] = baseState;
Int32 i = 1;
// now loop through and save each Style attribute for the List
foreach (ListItem li in this.Items)
{
Int32 j = 0;
string[][] attributes = new string[li.Attributes.Count][];
foreach (string attribute in li.Attributes.Keys)
{
attributes[j++] = new string[] { attribute, li.Attributes[attribute] };
}
allStates[i++] = attributes;
}
return allStates;
}
// restore base first
if (myState[0] != null)
base.LoadViewState(myState[0]);
Int32 i = 1;
foreach (ListItem li in this.Items)
{
// loop through and restore each style attribute
foreach (string[] attribute in (string[][])myState[i++])
{
li.Attributes[attribute[0]] = attribute[1];
}
}
}
}
}
}
mnasirkhan
Member
3 Points
17 Posts
How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 06:32 AM|LINK
Hi everyone!
I added attribute with my dropdownlist, but when page postback it lost its attribute state, how can i manage it. please help.
Thanx in advance
oned_gk
All-Star
31814 Points
6507 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 06:35 AM|LINK
shwetha.gadd...
Member
142 Points
65 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 06:40 AM|LINK
protected void Page_Load(object sender, EventArgs e)
{
if(!ispostback)
{SqlCommand cmk = new SqlCommand("Select StateName,StateId from StateMaster", con);
SqlDataAdapter dak = new SqlDataAdapter(cmk);
DataSet dsk = new DataSet();
dak.Fill(dsk);
stateControl.Items.Clear();
stateControl.DataSource = dsk;
stateControl.DataTextField = "StateName";
stateControl.DataValueField = "StateId";
stateControl.DataBind();
}
}
mnasirkhan
Member
3 Points
17 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 06:46 AM|LINK
Thanx guys for your reply, just attributes loses its state not datatextfield, datavaluefield and selectedindex....
shwetha.gadd...
Member
142 Points
65 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 07:01 AM|LINK
sry friend i acn't understand your problem... Can u pls post u r code once..
mnasirkhan
Member
3 Points
17 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 07:39 AM|LINK
mydropdown.DataSource = sortedList;
mydropdown.DataTextField = "Name";
mydropdown.DataValueField = "ID";
mydropdown.DataBind();
for (int i = 0; i < mydropdown.Items.Count; i++)
{
if (condition)
{
mydropdown.Items[i].Attributes.Add("style", "background-color:yellow");
mydropdown.Items[i].Attributes.Add("Title", "some text");
}
else
{
mydropdown.Items[i].Attributes.Add("style", "background-color:white");
mydropdown.Items[i].Attributes.Add("Title", "some other text here");
}
}
i lost this background color, tooltip etc when page postback
0ramramram0
Member
305 Points
73 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 07:46 AM|LINK
Look this
http://forums.asp.net/t/612172.aspx/1/10
http://codeasp.net/forums/asp-net-topics/clent-side-web-development/665/add-attributes-to-a-dropdown-list-and-read-the-attributes-on-client-side
shwetha.gadd...
Member
142 Points
65 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 01, 2013 08:14 AM|LINK
public void bin()
{
SqlCommand cmk = new SqlCommand("Select StateName,StateId from StateMaster", con);
SqlDataAdapter dak = new SqlDataAdapter(cmk);
DataSet dsk = new DataSet();
dak.Fill(dsk);
stateControl.Items.Clear();
stateControl.DataSource = dsk;
stateControl.DataTextField = "StateName";
stateControl.DataValueField = "StateId";
stateControl.DataBind();
stateControl.Items.Insert(0, new ListItem("Select State", "0"));
deta();
}
public void deta()
{
for (int i = 0; i < stateControl.Items.Count; i++)
{
if (i == 1 || i == 5)
{
stateControl.Items[i].Attributes.Add("style", "background-color:yellow");
stateControl.Items[i].Attributes.Add("Title", "some text");
}
else
{
stateControl.Items[i].Attributes.Add("style", "background-color:white");
stateControl.Items[i].Attributes.Add("Title", "some other text here");
}
}
}
protected void stateControl_SelectedIndexChanged(object sender, EventArgs e)
{
string s = stateControl.SelectedItem.Text;
deta();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bin();
}
}
mnasirkhan
Member
3 Points
17 Posts
Re: How to add attribute with dropdownlist, without losing its state.
Jan 02, 2013 07:00 AM|LINK
Problem has been solved by customizing dropdownlist as:
Steps:
1. create library
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Security.Permissions;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
namespace CustomControls
{
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
ToolboxBitmap(typeof(DropDownList)),
ToolboxData("<{0}:NewDropDownList runat=server></{0}:NewDropDownList>")]
public class NewDropDownList : DropDownList
{
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
protected override object SaveViewState()
{
// create object array for Item count + 1
object[] allStates = new object[this.Items.Count + 1];
// the +1 is to hold the base info
object baseState = base.SaveViewState();
allStates[0] = baseState;
Int32 i = 1;
// now loop through and save each Style attribute for the List
foreach (ListItem li in this.Items)
{
Int32 j = 0;
string[][] attributes = new string[li.Attributes.Count][];
foreach (string attribute in li.Attributes.Keys)
{
attributes[j++] = new string[] { attribute, li.Attributes[attribute] };
}
allStates[i++] = attributes;
}
return allStates;
}
protected override void LoadViewState(object savedState)
{
if (savedState != null)
{
object[] myState = (object[])savedState;
// restore base first
if (myState[0] != null)
base.LoadViewState(myState[0]);
Int32 i = 1;
foreach (ListItem li in this.Items)
{
// loop through and restore each style attribute
foreach (string[] attribute in (string[][])myState[i++])
{
li.Attributes[attribute[0]] = attribute[1];
}
}
}
}
}
}
2. Add this .dll to your project
3. Register assembly in your page as:
<%@ Register Assembly="CustomControls" Namespace="CustomControls" TagPrefix="cc1" %>
4. Enjoy your new dropdownlist....
<cc1:NewDropDownList ID="mydropdown" runat="Server">
</cc1:NewDropDownList>