這兩天做OA時用到GridView顯示資料。 為了提高使用者體驗。想寫一個單機行的事件。 結果發現GridView竟然沒有這個事件 只好求助百度大神了。
最終終於找到瞭解決辦法。好了 廢話不多說了。 貼上代碼
首先 在前台設定一個隱藏的ButtonField
<Columns>
<asp:ButtonField Visible="False" Text="SingleClick" CommandName="SingleClick" />
</Columns>
然後在RowDataBound和RowCommand事件裡寫上這些代碼。
protected void gvRoleInfos_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton _singleClickButton = (LinkButton)e.Row.Cells[0].Controls[0];
string _jsSingle = ClientScript.GetPostBackClientHyperlink(_singleClickButton, "");
e.Row.Attributes["onclick"] = _jsSingle;
}
}
protected void gvRoleInfos_RowCommand(object sender, GridViewCommandEventArgs e)
{
GridView _gridview = sender as GridView;
int _selectIndex = int.Parse(e.CommandArgument.ToString());
string _commandName = e.CommandName;
if (_commandName == "SingleClick")
{
_gridview.SelectedIndex = _selectIndex;
}
}
然後在本頁面重寫Render方法
protected override void Render(HtmlTextWriter writer)
{
foreach (GridViewRow row in gvRoleInfos.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Page.ClientScript.RegisterForEventValidation(row.UniqueID + "$ct100");
Page.ClientScript.RegisterForEventValidation(row.UniqueID + "$ct101");
Page.ClientScript.RegisterForEventValidation(row.UniqueID + "$ct102");
}
}
base.Render(writer);
}
原文地址:http://www.cnblogs.com/webabcd/archive/2007/04/22/723113.html
英文地址:http://www.codeproject.com/KB/webforms/EditGridviewCells.aspx