简体   繁体   English

如何以及何时为动态复选框保存“已检查”属性的值

[英]How and WHEN to save the values of the “Checked” attribute for Dynamic checkboxes

I am adding some checkboxes dynamically during runtime, and I need to know whether they are checked or not when I reload them next time. 我在运行时动态添加了一些复选框,下次我重新加载它们时,我需要知道是否选中了它们。 I load the checkbox values from a list stored in ViewState. 我从ViewState中存储的列表中加载复选框值。 The question is: when do I save or check for the value of the the Checked? 问题是:何时保存或检查Checked的值? I tried the event dispose for the check box and the place holder I am adding the checkboxes in, but it wasn't fired. 我尝试了将复选框和占位符添加到其中的事件处理,但是并未触发。 ie when I put a break point it didn't stop. 即,当我设置一个断点时,它并没有停止。 So any suggestions? 有什么建议吗?

This is a sample code, but I don't think it is necessary: 这是一个示例代码,但是我认为没有必要:

void LoadKeywords()
    {
        bool add = true;
        foreach (string s in (ViewState["keywords"] as List<string>))
            if (s == ddlKeywords.SelectedItem.Text)
            {
                add = false;
                continue;
            }

        if (add)
            (ViewState["keywords"] as List<string>).Add(ddlKeywords.SelectedItem.Text);



        foreach (string s in (ViewState["keywords"] as List<string>))
        {
            CheckBox kw = new CheckBox();
            kw.Disposed += new EventHandler(kw_Disposed);
            kw.Text = s;
            PlaceHolderKeywords.Controls.Add(kw);
        }
    }

If you are dynamically adding controls at run time you have to make sure that those controls are populated to the page's Control collection before ViewState is loaded. 如果要在运行时动态添加控件,则必须确保在加载ViewState之前将这些控件填充到页面的Control集合中。 This is so that the state of each checkbox can be rehydrated from Viewstate. 这样,每个复选框的状态都可以从Viewstate中重新添加。 The Page Load event, for example, is too late. 例如,页面加载事件为时已晚。

Typically you would dynamically add your CheckBox controls during the Init Event (before view state is loaded) and then Read the values in your Checkbox controls during the Load event (after view state is loaded). 通常,您将在Init事件期间(在加载视图状态之前)动态添加CheckBox控件,然后在Load事件期间(在加载视图状态之后)读取Checkbox控件中的值。

eg: 例如:

protected override void OnInit(EventArgs e)
{
    //load the controls before ViewState is loaded
    base.OnInit(e);
    for (int i = 0; i < 3; i++)
    {
        CheckBox cb = new CheckBox();
        cb = new CheckBox();
        cb.ID = "KeyWord" + i.ToString();
        cb.Text = "Key Word"
        MyPlaceHolder.Controls.Add(new CheckBox());
    }
}

//this could also be a button click event perhaps?
protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    if (Page.IsPostBack)
    {
        //read the checkbox values
        foreach(CheckBox control in MyPlaceHolder.Controls)
        {
            bool isChecked = control.Checked;
            string keyword = control.Text;

            //do something with these two values
        }
    }
}

Hope that helps 希望能有所帮助

****EDIT**** Forgot to mention that this is obviously just demo code - you would need to flesh it out. ****编辑****忘记提及这显然只是演示代码-您将需要充实它。

For more information on dynaic control rendering in ASP.Net check out this article on 4Guys . 有关ASP.Net中的动态控件渲染的更多信息,请参见4Guys 上的本文

For more information on the page life-cycle in ASP.Net check out MSDN . 有关ASP.Net中页面生命周期的更多信息,请查看MSDN

How to: 如何:

try adding a javascript code, that handles checked(), u can get the checkboxes by using document.findElementById(ID) , then store the checkboxe's value into a hiddenfield that has a runat="server" property. 尝试添加处理checked()的JavaScript代码,您可以使用document.findElementById(ID)来获取复选框,然后将复选框的值存储到具有runat =“ server”属性的隐藏字段中。

When to: either on pageload , check if page is postback(), and check the hiddenfield(s) value(S). 时间:在pageload上,检查page是否为postback(),并检查hiddenfield(s)的值。 or add a submit button (and place its event in the code behind, runat="server" property). 或添加一个提交按钮(并将其事件放在runat =“ server”属性后面的代码中)。

hope this helps u. 希望这对你有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM