Skip to content Skip to sidebar Skip to footer

Checkbox Status Not Saved When Update Panel Fires

I have an div id = campaignDiv and I load its content dynamically The content is checkboxes . I have an update panel that fires every 30 seconds. My problem when the update panel

Solution 1:

First off, adding runat="server" to your html string is meaningless here, as it will not be processed by the server and will be sent to the client as is.

Now, your update panel will always emit the same content because its repopulated from the ViewState, if you disable the update panel view state, on the next refresh it will return an empty panel (there is no view state to read from)

So what to do here! You need to refresh the update panel contents on post back, and manually read the client state.

If view state size does concern you, then do the following:

publicpartialclassWebForm4 : System.Web.UI.Page
{
  privatestringCreateLiCheckbox(string checkBoxText)
  {
    var checkState = !string.IsNullOrEmpty(Request.Form[string.Format("chk_{0}", checkBoxText)]) ? "checked=\"checked\"" : "";

    returnstring.Format("<li><span class=\"textDropdown\">{0}</span><input id=\"{1}\" name=\"chk_{0}\" value=\"{0}\" type=\"checkbox\" {2}><label for=\"{1}\"></label></li>",
      checkBoxText,
      checkBoxText + "dropdownID",
      checkState);
  }
  protectedvoidPage_Load(object sender, EventArgs e)
  {
    int refreshtime = 5000;
    Timer1.Interval = refreshtime;

    if (!IsPostBack)
      PopulateCheckboxes();
  }
  protectedvoidTimer1_Tick(object sender, EventArgs e)
  {
    PopulateCheckboxes();
  }
  privatevoidPopulateCheckboxes()
  {
    string[] comps = newstring[] { "default", "sales", "direct" };
    string html = "<ul>";
    for (int i = 0; i < comps.Count(); i++)
    {
      html = html + CreateLiCheckbox(comps[i]);
    }
    html = html + "</ul>";
    campaignDiv.InnerHtml = html;
  }
}

Solution 2:

To have more space for code here is my suggestion for the checkboxlist again. I hope it's that what you are asking for.

.aspx

<asp:UpdatePanelID="upStuff"runat="server"UpdateMode="Conditional"><ContentTemplate><divid="campaignDiv"><asp:CheckBoxListrunat="server"RepeatLayout="UnorderedList"ID="myCheckboxList"TextAlign="Left"></div></asp:CheckBoxList></ContentTemplate><Triggers><asp:AsyncPostBackTriggerControlID="Timer1"EventName="Tick"/></Triggers></asp:UpdatePanel>

.aspx.cs

if (!IsPostBack)
{
    var comps = new[] { "default", "sales", "direct" };
    for (int i = 0; i < comps.Count(); i++)
    {
         myCheckboxList.Items.Add(new ListItem{Text = comps[i]});
    }
}

This way it will keep your checkbox checked

Post a Comment for "Checkbox Status Not Saved When Update Panel Fires"