gridview為什麼分頁後,GridView1_RowDataBound就運行不了
在GridView1_RowDataBound事件中:
Dim str As String = "demand_info.aspx?id=" + Server.UrlEncode("歡迎光臨!!")
e.Row.Cells(1).Text = "<a href=" + str.Trim + ">" + e.Row.Cells(1).Text + "</a>"
e.Row.Cells(2).Text = "<a href=" + str.Trim + ">" + e.Row.Cells(2).Text + "</a>"
e.Row.Cells(3).Text = "<a href=" + str.Trim + ">" + e.Row.Cells(3).Text + "</a>"
如果不分頁,是可以運行過去的,但是一旦分頁,就出錯,出錯原因是:
Specified argument was out of the range of valid values. Parameter name: index
---------------------------------------------------------------------------
因為改成分頁後會多一個Pager的Row
Pager的Row沒有Cells(2)跟Cells(3)所以會超出索引範圍
把程式改成這樣
Pager的Row沒有Cells(2)跟Cells(3)所以會超出索引範圍
把程式改成這樣
1 | If e.Row.RowType = DataControlRowType.DataRow Then |
2 | Dim str As String = "demand_info.aspx?id=" + Server.UrlEncode("歡迎光臨!!") |
3 | e.Row.Cells(1).Text = "<a href=" + str.Trim + ">" + e.Row.Cells(1).Text + "</a>" |
4 | e.Row.Cells(2).Text = "<a href=" + str.Trim + ">" + e.Row.Cells(2).Text + "</a>" |
5 | e.Row.Cells(3).Text = "<a href=" + str.Trim + ">" + e.Row.Cells(3).Text + "</a>" |
6 | End If |
看不清楚 | 列印 | 複製 |