1
protected void Page_Load(object sender, EventArgs e)
2
{
3
if (!IsPostBack)
4
{
5
GridView_DataBind();
6
}
7
}
8
private void GridView_DataBind()
9
{
10
DataTable DT = new DataTable();
11
string DBString =
12
"select account_phone,account_goods from t_account_all";
13
DT = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text,DBString);
14
Click_GridView.DataSource = DT;
< /span>15< span style="color: #000000;">
//这里要绑定的GridView
是:Click_GridView
16
Click_GridView.DataBind();
17
}
给GridView的行和单元格绑定数据,在RowDataBound事件中进行,对于需要根据具体数据值绑定的时候效果最好:
2

3

4

5

6

7

8

9

10

11

12

13

14

< /span>15< span style="color: #000000;">

16

17

1
protected void Click_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
2
{
3
if ((e.Row.RowIndex != -1) && (e.Row.RowType == DataControlRowType.DataRow))
4
{
< /span> 5
/* 单元格绑定:
6< span style="color: #008000;">
* 绑定属性onclick
< span style="color: #008080;user-select:none;"> 7
* 如果值为self.location那么表示在当前窗口打开新页面<
br />
&
nbsp;8
* 如果值为window.open那么表示在新的窗口打开页面
9
*/
10
e.Row.Cells[0].Attributes.Add("onclick", /*"self.location='"*/"window.open('" + "../Default.aspx?" + HttpUtility.UrlEncode("Phone=" + ((HyperLink)(e.Row.Cells[0].Controls[1])).Text+"')"));
11
e.Row.Cells[0].Attributes.Add("onmouseover", "this.style.background='#f70'");
12
e.Row.Cells[0].Attributes.Add("onmouseout", "this.style.background='#fff'");
< /span>13< span style="color: #000000;">
/* 行级绑定:
< span style="color: #008080;user-select:none;">14
* 点击行就执行跳转
15
*/
16
e.Row.Attributes.Add("onclick", "window.open('../Default.aspx?" + HttpUtility.UrlEncode("Id=" + ((HyperLink)(e.Row.Cells[1].Controls[1])).Text) + "')");
< /span>17< span style="color: #000000;">
/* 在行级和单元格级同时绑定
18
* 在点击单元格1时,
< span style="color: #008080;user-select:none;">19
* 同时激发两个事件
< span style="color: #008080;user-select:none;">20
* 其中,单元格自己的事件在前
21
* 行级事件在后
22
* 两者不发生冲突
23
*/
24
25
}
GridView的RowDataBound另一个示例:
2

3

4

< /span> 5

6< span style="color: #008000;">

< span style="color: #008080;user-select:none;"> 7


9

10

11

12

< /span>13< span style="color: #000000;">

< span style="color: #008080;user-select:none;">14

15

16

< /span>17< span style="color: #000000;">

18

< span style="color: #008080;user-select:none;">19

< span style="color: #008080;user-select:none;">20

21

22

23

24

1
protected void DataBound_GridView_RowDataBound(object sender, GridViewRowEventArgs e)
2
{
< /span> 3
实现1
}
2

< /span> 3

25
