根據使用者需要,一個頁面中,有幾個不同的button,點擊不同的button,可在下方同一地區產生gridview,(當然他們的gridview是不同的)。之前用MultiView來控制,用了幾個view在html中綁定了幾個Gridview,然後根據點擊的按鈕不同,切換不同的view。但總覺得這種方式不爽~~,html代碼太多,控制起來很麻煩~!
於是就想到用這種動態方式實現。Html中只有一個div,用來放置產生的控制項,其他全部在後台實現。
有一點需要注意:如果是在html中加入的控制項,頁面回傳前後系統都會對其狀態和屬性做viewstate處理,所以,回傳時,根據頁面生命週期:頁面重新初始化並載入控制項執行個體,而後loadViewstate載入控制項屬性和狀態,之後回傳前控制項狀態重新出現。但是對於動態載入的控制項,viewstate記住了他們的屬性和狀態,但是並沒有儲存控制項本身。所以回傳後,控制項也就不見了~
鑒於此,有多種處理辦法,但是歸根到底,都需要在頁面回傳時的初始化狀態下對動態控制項重新載入。我這裡所做的是在page_load事件中,對其重新載入的,當然也可以在更早的事件中做。
說明: 因為是幾個不同的載入,所以這裡做個一個sign標記(用的static string, 也可以用屬性),從而在重新載入的時候判斷到底載入哪個gridview.
廢話不多說,看代碼:
HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>無標題頁</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="list">
<asp:Button ID="btngv" runat="server" Text="產生GridView 1" onclick="btngv_Click" />
<asp:Button ID="Button1" runat="server" Text="產生GridView 2" onclick="Button1_Click" />
</div>
<div id="divshow" runat="server" >
<%...--在這裡放置動態產生的gridview--%>
</div>
</div>
</form>
</body>
</html>
CS:
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
...{
GridView gvshow=null;
public static string sign;
protected void Page_Load(object sender, EventArgs e)
...{
if (sign != null) //根據標記符號來判斷,重新綁定那個控制項
...{
if (sign == "gv1")
setBind();
else
setBind2();
}
}
public void setBind()
...{
DataTable dt = getDataTable(); //獲得資料來源
gvshow = new GridView();
gvshow.Width = Unit.Pixel(700);
gvshow.AutoGenerateColumns = false;
gvshow.RowEditing += new GridViewEditEventHandler(gvshow_RowEditing); //添加編輯事件
gvshow.RowDeleting +=new GridViewDeleteEventHandler(gvshow_RowDeleting);
gvshow.DataSource = dt;
for (int i = 0; i < dt.Columns.Count; i++)
...{
BoundField bc = new BoundField();
bc.DataField = dt.Columns[i].ColumnName.ToString();
bc.HeaderText = dt.Columns[i].Caption.ToString();
gvshow.Columns.Add(bc);
}
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Link;
cf.ShowEditButton = true;
cf.CausesValidation = false;
gvshow.Columns.Add(cf);
CommandField cf2 = new CommandField();
cf2.ButtonType = ButtonType.Link;
cf2.ShowDeleteButton = true;
cf2.CausesValidation = false;
gvshow.Columns.Add(cf2);
gvshow.DataBind();
divshow.Controls.Add(gvshow);
}
public void setBind2()
...{
DataTable dt = getDataTable(); //獲得資料來源
gvshow = new GridView();
gvshow.Width = Unit.Pixel(700);
gvshow.AutoGenerateColumns = false;
gvshow.RowDeleting +=new GridViewDeleteEventHandler(gvshow_RowDeleting); //添加刪除事件
gvshow.DataSource = dt;
for (int i = 0; i < dt.Columns.Count; i++)
...{
BoundField bc = new BoundField();
bc.DataField = dt.Columns[i].ColumnName.ToString();
bc.HeaderText = dt.Columns[i].Caption.ToString();
gvshow.Columns.Add(bc);
}
CommandField cf = new CommandField();
cf.ButtonType = ButtonType.Link;
cf.ShowDeleteButton = true;
cf.CausesValidation = false;
gvshow.Columns.Add(cf);
gvshow.DataBind();
divshow.Controls.Add(gvshow);
}
void gvshow_RowDeleting(object sender, GridViewDeleteEventArgs e)
...{
int i = e.RowIndex;
Response.Write("delete:" + gvshow.Rows[i].Cells[0].Text.ToString());
}
protected void btngv_Click(object sender, EventArgs e) //條件選擇後,點擊產生按鈕,產生特定的gridview
...{
divshow.Controls.Clear();
setBind();
sign = "gv1";
}
public void gvshow_RowEditing(object sender, GridViewEditEventArgs e)
...{
int i = e.NewEditIndex;
string id = gvshow.Rows[i].Cells[0].Text.ToString();
Response.Write("you want to edit"+id);
}
public DataTable getDataTable()
...{
DAL.ISDApp01 cDal = new DAL.ISDApp01();
string sql = "select * from test";
DataTable dt = cDal.ExecuteQuery(sql).Tables[0];
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
...{
divshow.Controls.Clear();
setBind2();
sign = "gv2";
}
}
文章出處:http://www.diybl.com/course/4_webprogram/asp.net/netjs/2008520/117330.html