如何防止在Gridview创建的控件在回发后消失

来源:互联网 发布:ubuntu怎么删除用户 编辑:程序博客网 时间:2024/05/06 23:28

本文主要讲述如何实现在标题头上放一个按钮上去,按下去选择一整个列的功能。用三种方法演示效果。

 

方法一(较简单):

使用模板列

 

HTML code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns=FalseWidth="230px">
   
<Columns>
       
<asp:TemplateField>
           
<HeaderTemplate>
               
<asp:ButtonID="Button1" runat="server" Text="全选" OnClick="Button1_Click"/>
           
</HeaderTemplate>
           
<ItemTemplate>
               
<%#Eval("id")%>
           
</ItemTemplate>
       
</asp:TemplateField>
       
<asp:BoundFieldDataField="title"/>
   
</Columns>
</asp:GridView>
C# code
protected void Page_Load(object sender, EventArgs e)
{
   
if (!IsPostBack)
    {
        DataTable dt
=new DataTable();
        dt.Columns.Add(
"title");
        dt.Columns.Add(
"id");
        dt.Rows.Add(
"sdafsa","1");
        dt.Rows.Add(
"sdafsa","2");
        dt.Rows.Add(
"sdafsa","3");
        dt.Rows.Add(
"sdafsa","4");
        GridView1.DataSource
= dt;
        GridView1.DataBind();
    }
}
protected void Button1_Click(object sender, EventArgs e)
{
    Button bt
= (Button)sender;
   
if (bt.Text== "全选")
       
for (int i= 0; i< GridView1.Rows.Count; i++)
        {
            GridView1.Rows[i].Cells[
0].Style.Add("background-color","red");
            bt.Text
="取消";
        }
   
else
       
for (int i= 0; i< GridView1.Rows.Count; i++)
        {
            GridView1.Rows[i].Cells[
0].Style.Add("background-color","white");
            bt.Text
="全选";
        }
}

 

 

方法二(中等难度):
在Page_Load中的if (!IsPostBack){}外边动态添加控件

 

 

HTML code
<asp:GridView ID="GridView1" runat="server"  Width="230px" OnRowDataBound="GridView1_OnRowDataBound">
</asp:GridView>

 

C# code
protected void Page_Load(object sender, EventArgs e)
{
   
if (!IsPostBack)
    {
        DataTable dt
=new DataTable();
        dt.Columns.Add(
"title");
        dt.Columns.Add(
"id");
        dt.Rows.Add(
"sdafsa","1");
        dt.Rows.Add(
"sdafsa","2");
        dt.Rows.Add(
"sdafsa","3");
        dt.Rows.Add(
"sdafsa","4");
        GridView1.DataSource
= dt;
        GridView1.DataBind();
    }
    Button bt
=new Button();
    //bt.ID
="bt1";
    bt.Text
="测试";
    bt.CommandArgument
="1";
    bt.Click
+=new EventHandler(bt_Click);
    GridView1.HeaderRow.Cells[
1].Controls.Add(bt);
}
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
   
if (e.Row.RowType== DataControlRowType.Header)
    {
       
//Button bt = new Button();
       
//bt.ID = "bt1";
       
//bt.Text = "测试";
       
//bt.CommandArgument = "1";
       
//bt.Click+=new EventHandler(bt_Click);
       
//e.Row.Cells[1].Controls.Add(bt);
    }
}
protected void bt_Click(object sender, EventArgs e)
{
    Button bt
= (Button)sender;
   
if (bt.CommandArgument== "1")
       
for (int i= 0; i< GridView1.Rows.Count; i++)
        {
            GridView1.Rows[i].Cells[
0].Style.Add("background-color","red");
            bt.CommandArgument
="0";
        }
   
else
       
for (int i= 0; i< GridView1.Rows.Count; i++)
        {
            GridView1.Rows[i].Cells[
0].Style.Add("background-color","white");
            bt.CommandArgument
="1";
        }
}

 

 

 

第三种(有点难度):

使用viewstate保存序列化后的控件,在创建时再反序列化

 

HTML code

<asp:GridView ID="GridView1" runat="server"  Width="230px"  OnLoad="GridView1_Load">
</asp:GridView>

 

C# code  

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("title");
            dt.Columns.Add("id");
            dt.Rows.Add("sdafsa", "1");
            dt.Rows.Add("sdafsa", "2");
            dt.Rows.Add("sdafsa", "3");
            dt.Rows.Add("sdafsa", "4");
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }
    protected void GridView1_Load(object sender, EventArgs e)
    {

            oButton bt = new oButton();
            if (ViewState["but"] == null)
            {
                bt.ID = "bt1";
                bt.Text = "测试";
                bt.arg = "1";          
                ViewState["but"] = bt;
            }
            else
                bt = ViewState["but"] as oButton;
            bt.Click += new EventHandler(bt_Click);
            GridView1.HeaderRow.Cells[1].Controls.Add(bt);

    }
    protected void bt_Click(object sender, EventArgs e)
    {
        oButton bt = (oButton)sender;
        if (bt.arg == "1")
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].Cells[0].Style.Add("background-color", "red");
                bt.arg = "0";
            }
        else
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridView1.Rows[i].Cells[0].Style.Add("background-color", "white");
                bt.arg = "1";
            }
        ViewState["but"] = bt;
    }
这是重点,重写button类,并继承ISerializable接口,以实现button的可序列化:
[Serializable]
class oButton : Button, System.Runtime.Serialization.ISerializable
{
    public string arg;
    public oButton() { }
    protected oButton(System.Runtime.Serialization.SerializationInfo info,
        System.Runtime.Serialization.StreamingContext context)
    {
        this.ID = info.GetString("id");
        this.Text = info.GetString("txt");
        this.arg = info.GetString("arg");
    }

    public void GetObjectData(System.Runtime.Serialization.SerializationInfo info,
        System.Runtime.Serialization.StreamingContext context)
    {
        info.AddValue("id", this.ID);
        info.AddValue("txt", this.Text);
        info.AddValue("arg", this.arg);
    }

}