动态添加控件和按钮回发

时间:2015-01-22 13:03:03

标签: c# asp.net

您好我的GridView有问题。 假设我创建了一个包含4列的GridView

  

| NAME | LASTNAME | OWNER |管理员|

所有者和管理员是可以包含复选框的列。 第二步:我以某种方式填充网格。 第三步:我动态添加所有者和管理列上的复选框,具体取决于一些奇怪的方法。 四步:当我单击gridview下面的Button时,click事件将读取检查了多少复选框并将该数字传递给另一个方法。

这最后一步非常困难,因为所有复选框都会因按钮的回发而消失。 我正在寻找一种方法来保存回发中的复选框。

我可以在点击之后和回发之前调用方法吗? 我可以避免按钮回发吗?

(PS:我更愿意不去寻求解决方案)

2 个答案:

答案 0 :(得分:0)

如果您放置方法调用以动态添加Page_Init中的复选框,则不会在回发时删除它们

答案 1 :(得分:0)

当你可以在GridView的RowDataBound事件中设置状态服务器端时,你的问题不清楚为什么你需要动态添加CheckBox控件。以下是您的要求的一个有效例子,最简单的说明是:

CheckboxGrid.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CheckboxGrid.aspx.cs" Inherits="CheckboxGrid" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvResourceUsers" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvResourceUsers_OnRowDataBound">
            <Columns>
                <asp:BoundField HeaderText="Name" DataField="Name"/>
                <asp:BoundField HeaderText="Surname" DataField="Surname"/>
                <asp:TemplateField HeaderText="Owner">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbxOwner" runat="server"/>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Administrator">
                    <ItemTemplate>
                        <asp:CheckBox ID="cbxAdministrator" runat="server"/>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:Button runat="server" ID="btnSubmit" Text="Save" OnClick="btnSubmit_Click"/>
    </div>
    </form>
</body>
</html>

CheckboxGrid.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class CheckboxGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Get data and bind the grid
            gvResourceUsers.DataSource = GetData();
            gvResourceUsers.DataBind();
        }
    }

    protected void gvResourceUsers_OnRowDataBound(Object sender, GridViewRowEventArgs e)
    {
        //As each row is data-bound, set the checkbox state.
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var resourceUser = e.Row.DataItem as ResourceUser;
            var cbxOwner = e.Row.FindControl("cbxOwner") as CheckBox;
            var cbxAdministrator = e.Row.FindControl("cbxAdministrator") as CheckBox;

            cbxOwner.Checked = resourceUser.Owner;
            cbxAdministrator.Checked = resourceUser.Administrator;
        }
    }

    protected void btnSubmit_Click(Object sender, EventArgs e)
    {
        var resourceUsers = new List<ResourceUser>();

        //Iterate the gridview rows and populate the collection from the postback data.
        foreach (GridViewRow row in gvResourceUsers.Rows)
        {
            resourceUsers.Add(
                new ResourceUser
                {
                    Name = row.Cells[0].Text,
                    Surname = row.Cells[1].Text,
                    Owner = ((CheckBox)row.Cells[2].FindControl("cbxOwner")).Checked,
                    Administrator = ((CheckBox)row.Cells[3].FindControl("cbxAdministrator")).Checked
                });
        }
    }

    private IEnumerable<ResourceUser> GetData()
    {
        //We just create some data for demo purposes. Here you would normally populate the collection from your database.
        var resourceUsers = new List<ResourceUser>
            {
                new ResourceUser{Name = "Bob", Surname = "Taylor", Owner = true, Administrator = true },
                new ResourceUser{Name = "Ann",  Surname = "Carter", Owner = false, Administrator = true },
                new ResourceUser{Name = "Toni",  Surname = "Wong", Owner = false, Administrator = false}
            };

        return resourceUsers;
    }

    //A data view model to contain our view data for the grid
    private class ResourceUser
    {
        public String Name { get; set; }

        public String Surname { get; set; }

        public Boolean Owner { get; set; }

        public Boolean Administrator { get; set; }
    }
}