checkbox status not saved when update panel fires

前端 未结 2 1948
南方客
南方客 2021-01-25 01:15

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.

2条回答
  •  礼貌的吻别
    2021-01-25 01:52

    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:

    public partial class WebForm4 : System.Web.UI.Page
    {
      private string CreateLiCheckbox(string checkBoxText)
      {
        var checkState = !string.IsNullOrEmpty(Request.Form[string.Format("chk_{0}", checkBoxText)]) ? "checked=\"checked\"" : "";
    
        return string.Format("
  • {0}
  • ", checkBoxText, checkBoxText + "dropdownID", checkState); } protected void Page_Load(object sender, EventArgs e) { int refreshtime = 5000; Timer1.Interval = refreshtime; if (!IsPostBack) PopulateCheckboxes(); } protected void Timer1_Tick(object sender, EventArgs e) { PopulateCheckboxes(); } private void PopulateCheckboxes() { string[] comps = new string[] { "default", "sales", "direct" }; string html = "
      "; for (int i = 0; i < comps.Count(); i++) { html = html + CreateLiCheckbox(comps[i]); } html = html + "
    "; campaignDiv.InnerHtml = html; } }

提交回复
热议问题