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
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():Then have a function that always runs:
Then in your
Page_Load:** 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.