gridview 是 asp.net 2.0 中應用最為廣泛一個控件,幾乎所有的數據操作都需要它,正如我們平常所應用的,可以編輯、刪除、選擇等等,但如果客戶有需要通過單擊行而引發超鏈接或者進入行編輯狀態時,我們該如何實現,這里介紹了一種方法來實現此功能。它將允許你通過點擊行的任何一個位置而引發你所需要的事件。
首先為 gridview 填充數據
private void binddata()
{
sqlconnection myconnection = new sqlconnection(connectionstring);
sqlcommand mycommand = new sqlcommand("select * from users", myconnection);
sqldataadapter ad = new sqldataadapter(mycommand);
dataset ds = new dataset();
ad.fill(ds);
gridview1.datasource = ds;
gridview1.databind();
}
接下來我們在 gridview_rowdatabound 事件中為 gridviewrow 賦予單擊屬性
protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)
{
string alertbox = "alert('";
if (e.row.rowtype == datacontrolrowtype.datarow)
{
alertbox += e.row.rowindex;
alertbox += "')";
e.row.attributes.add("onclick", alertbox);
}
}
好了,很簡單的方法,希望對你有用!
新聞熱點
疑難解答