我使用GridView來顯示數據庫中的記錄。 另外我已經使用TemplateField將超鏈接附加到GridView。 當我嘗試添加「點擊」屬性,以超鏈接的RowDateBound事件,我碰到下面的錯誤裏面..ArgumentOutOfRangeException由用戶代碼未處理 - (ASP .net)
GridView1.DataKeys[e.Row.RowIndex].Value = 'GridView1.DataKeys[e.Row.RowIndex]' threw an exception of type 'System.ArgumentOutOfRangeException'
消息=「索引超出範圍。必須爲非負值並小於集合的大小\ r \ n參數名:索引」
這是的RowDataBound mentod內部編碼..
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink HyperLink1 = (HyperLink)e.Row.FindControl("HyperLink1");
//The following line makes the error
HyperLink1.Attributes.Add("onclick", "ShowMyModalPopup('" + GridView1.DataKeys[e.Row.RowIndex].Value + "')");
}
}
這是 「ShowMyModalPopup」 功能在JavaScript
<script type="text/javascript">
function ShowMyModalPopup(userpk)
{
var modal = $find('ModalPopupExtender1');
modal.show();
WebService.FetchOneUser(userpk,DisplayResult);
}
</script>
雖然索引值爲零,但會拋出此異常。爲什麼這個錯誤發生 誰能請解釋我..
提前很多感謝..
我怎樣才能做到這一點.. 能否請您指導我.. 我已經向我的「用戶」表「userPK」傳遞給ShowMyModalPopup() – 2010-06-07 06:56:27
好吧,如果它已經進入GridView控件(通過你指定的任何查詢),你所需要做的就是使用代碼:'e.Row [「userPK」]。Value'來獲取超鏈接的值。如果它尚未進入GridView進行顯示,則需要更新查詢(或自動生成的DataTable)以包含該查詢。我希望這是有道理的;不知道具體情況很難解釋。 – drharris 2010-06-07 12:55:58