<
script runat
=
"
server
"
>
protected
void
Button1_Click(
object
sender, System.EventArgs e)
{
GridView1.DataSource
=
GetData();
GridView1.DataBind();
}
protected
void
Button2_Click(
object
sender, System.EventArgs e)
{
string
s
=
String.Empty;
for
(
int
i
=
0
; i
<
5
; i
++
)
{
for
(
int
j
=
0
; j
<
5
; j
++
)
{
s
=
s
+
"
<li>第
"
+
i.ToString()
+
"
行第
"
+
j.ToString()
+
"
列的值是:
"
+
Request.Form[
"
txt
"
+
i.ToString()
+
"
_
"
+
j.ToString()];
}
}
ret.Text
=
s;
//
以下代码只是为了实现在表格里面保留原来的值,如果不需要则可以删除。
GridView1.DataSource
=
GetData();
GridView1.DataBind();
}
protected
void
GridView1_RowDataBound(
object
sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
System.Data.DataRowView dv
=
(System.Data.DataRowView)e.Row.DataItem;
for
(
int
i
=
0
; i
<
e.Row.Cells.Count; i
++
)
{
e.Row.Cells[i].Attributes.Add(
"
onclick
"
,
"
showEdit(
"
+
e.Row.RowIndex.ToString()
+
"
,
"
+
i.ToString()
+
"
)
"
);
e.Row.Cells[i].Text
=
"
<input onblur='lostfocus(this)' name='txt
"
+
e.Row.RowIndex.ToString()
+
"
_
"
+
i.ToString()
+
"
' readonly='readonly' class='noborder' value='
"
+
dv[i].ToString()
+
"
'/>
"
;
}
}
}
private
System.Data.DataTable GetData()
{
System.Data.DataTable dt
=
new
System.Data.DataTable();
for
(
int
i
=
0
; i
<
5
; i
++
)
{
dt.Columns.Add(
new
System.Data.DataColumn(
""
,
typeof
(System.String)));
}
for
(
int
i
=
0
; i
<
5
; i
++
)
{
dt.Rows.Add(dt.NewRow());
for
(
int
j
=
0
; j
<
5
; j
++
)
{
dt.Rows[i][j]
=
Request.Form[
"
txt
"
+
i.ToString()
+
"
_
"
+
j.ToString()];
}
}
return
dt;
}
</
script
>
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head id
=
"
Head1
"
runat
=
"
server
"
>
<
title
></
title
>
<
style type
=
"
text/css
"
>
.noborder{border
-
width: 0px;margin:2px;}
.hasborder{border
-
width: 2px;}
</
style
>
<
script type
=
"
text/javascript
"
>
function showEdit(r, c) {
document.forms[
0
].elements[
"
txt
"
+
r
+
"
_
"
+
c].readOnly
=
false
;
document.forms[
0
].elements[
"
txt
"
+
r
+
"
_
"
+
c].className
=
'
hasborder
'
;
document.forms[
0
].elements[
"
txt
"
+
r
+
"
_
"
+
c].select();
}
function lostfocus(o) {
o.className
=
'
noborder
'
o.readOnly
=
true
;
}
</
script
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
asp:GridView ID
=
"
GridView1
"
runat
=
"
server
"
ShowHeader
=
"
false
"
OnRowDataBound
=
"
GridView1_RowDataBound
"
>
</
asp:GridView
>
<
asp:Button ID
=
"
Button1
"
runat
=
"
server
"
OnClick
=
"
Button1_Click
"
Text
=
"
加载表格
"
/>
<
asp:Button ID
=
"
Button2
"
runat
=
"
server
"
Text
=
"
保存数据
"
OnClick
=
"
Button2_Click
"
/>
<
asp:Label ID
=
"
ret
"
runat
=
"
server
"
></
asp:Label
>
</
form
>
</
body
>
</
html
>