Postback Removing Styling from Page
Currently I've created a ASP.Net page that has a dropdown control with autopostback set to true. I've also added color backgrounds for individual listitems. Whenever an item is selected in the dropdown control the styling is completely removed from all of the list items. How can I prevent this from happening? I need the postback to pull data based on the dropdown item that is selected. Here is my code.
aspx file:
<asp:DropDownList ID="EmpDropDown" AutoPostBack="True" OnSelectedIndexChanged="EmpDropDown_SelectedIndexChanged" runat="server">
</asp:DropDownList>
<asp:TextBox ID="MessageTextBox" TextMode="MultiLine" Width="550" Height="100px" runat="server"></asp:TextBox>
aspx.cs code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetEmpList();
}
}
protected void EmpDropDown_SelectedIndexChanged(object sender,开发者_开发百科 EventArgs e)
{
GetEmpDetails();
}
private void GetEmpList()
{
SqlDataReader dr = ToolsLayer.GetEmpList();
int currentIndex = 0;
while (dr.Read())
{
EmpDropDown.Items.Add(new ListItem(dr["Title"].ToString(), dr["EmpKey"].ToString()));
if (dr["Status"].ToString() == "disabled")
{
EmpDropDown.Items[currentIndex].Attributes.Add("style", "background-color:red;");
}
currentIndex++;
}
dr.Close();
}
private void GetEmpDetails()
{
SqlDataReader dr = ToolsLayer.GetEmpDetails(EmpDropDown.SelectedValue);
while (dr.Read())
{
MessageTextBox.Text = dr["Message"].ToString();
}
dr.Close();
}
Thank You
You might want to have a look at this blog entry: ListItem, attributes and viewstate....
It comes down to write a custom list control (DropDownList
in your case, I think) that explicitly saves the attribute(s) in viewstate. You might even consider using control state, I guess.
The article mentions that this behaviour is "by design" (a.k.a. a feature) but does not mention why the decision has been made. If anybody knows, I would be interested in the "why" :)
Here is a quick and dirty way of doing your own ViewState without having to make a custom control to persist the attriibute at the item level which the control by default does not do.
You could change your code to do the following in GetEmpList()
:
private void GetEmpList()
{
List<string> disabledKeys = new List<string>();
SqlDataReader dr = ToolsLayer.GetEmpList();
while (dr.Read())
{
EmpDropDown.Items.Add(new ListItem(
dr["Title"].ToString(), dr["EmpKey"].ToString()));
if (dr["Status"].ToString() == "disabled")
{
disabledKeys.Add(dr["EmpKey"].ToString());
}
}
dr.Close();
ViewState["DisabledKeys"] = disabledKeys;
}
Then have a function that always runs:
public void SetDisabledStylingOnDropDown()
{
if ((ViewState["DisabledKeys"] != null) &&
(EmpDropDown.Items.Count > 0))
{
List<string> disabledKeys = (List<string>)(ViewState["DisabledKeys"]);
for (int i = 0; i < EmpDropDown.Items.Count; i++)
{
if (disabledKeys.Contains(EmpDropDown.Items[i].Value))
{
EmpDropDown.Items[i].Attributes.Add("style", "background-color:red;");
}
}
}
}
Then in your Page_Load
:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetEmpList();
}
SetDisabledStylingOnDropDown();
}
** Note: there could be compile errors as I just typed this without running it through any compiler. You should get the idea of what I am trying to do based on the changes above.
The problem is likely that the attributes are not being persisted in the viewstate, so they can't be read from the viewstate and re-added to the controls on postback.
A quick search led me to this article, which I didn't fully read but seems to describe (and resolve) the situation you have. It doesn't look trivial.
https://web.archive.org/web/20211020153240/https://www.4guysfromrolla.com/articles/110205-1.aspx
I think your problem is that you add the styling programatically, and when you post back these attributes are lost, they are not saved between page loads in the viewstate. (That is my guess).
Your solution would be creating a method that would loop the contents of your dropdown if any, and re-apply the style and call it everytime you fire an event.
You may want to implement some kind of loading/saving mechanism for these attributes in the viewstate, since re-applying these from scratch would involve another query every page, but unless you have many postbacks maybe you'll do fine.
精彩评论