动态创建复选框控制提交时丢失的数据

时间:2014-01-22 06:33:11

标签: asp.net .net checkbox

我在服务器端动态创建了复选框控件,当用户选中/取消选中复选框并提交时。 干净的HTML代码就是那样

<form id="form1" runat="server" method="post">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>  
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Panel ID="pnlGroupAdd" runat="server" DefaultButton="Publish">
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                <asp:Button Text="Publish" runat="server" ID="Submit" OnClick="Submit_Click" />
            </asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>


public partial class Premission : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            XmlDocument doc = new XmlDocument();
            // Code for fetch doc and datatable dt etc

            foreach (XmlNode node in doc.DocumentElement.SelectNodes("/config/permission"))
            {

                string text = node.InnerText; //or loop through its children as well
                CheckBox cbModels = new CheckBox();
                cbModels.ID = "cbPremissionID_" + node["id"].InnerText;
                cbModels.Text = node["displayName"].InnerText + "<br/>";
                cbModels.Checked = false;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (cbModels.ID.Trim() == "cbPremissionID_" + dt.Rows[i][0].ToString().Trim())
                    {
                        cbModels.Checked = true;
                        dt.Rows.RemoveAt(i);
                        break;
                    }
                }
                PlaceHolder1.Controls.Add(cbModels);

            }           
        }
    }
    protected void Submit_Click(object sender, EventArgs e)
    {
        if (PlaceHolder1.Controls.Count > 0)
            {
                foreach (Control item in PlaceHolder1.Controls)
                {
                    if (item.GetType().FullName == "System.Web.UI.WebControls.CheckBox")
                    {
                        // submit check box data to database
                    }
                }
            }

    }
}

在Submit_Click上无法访问复选框数据。

1 个答案:

答案 0 :(得分:0)

您应该在Page_Init事件之前在Page_Load之前初始化动态控件。

请阅读有关asp.net页面生命周期的内容,它可以帮助您避免将来的许多痛苦:http://msdn.microsoft.com/en-us/library/ms178472.aspx

protected void Page_Init(object sender, EventArgs e)
{
        XmlDocument doc = new XmlDocument();
        // Code for fetch doc and datatable dt etc

        foreach (XmlNode node in doc.DocumentElement.SelectNodes("/config/permission"))
        {

            string text = node.InnerText; //or loop through its children as well
            CheckBox cbModels = new CheckBox();
            cbModels.ID = "cbPremissionID_" + node["id"].InnerText;
            cbModels.Text = node["displayName"].InnerText + "<br/>";
            cbModels.Checked = false;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (cbModels.ID.Trim() == "cbPremissionID_" + dt.Rows[i][0].ToString().Trim())
                {
                    cbModels.Checked = true;
                    dt.Rows.RemoveAt(i);
                    break;
                }
            }
            PlaceHolder1.Controls.Add(cbModels);

        }
}