JS访问GridView中的模板控件
时间:2010-10-11 来源:程序帝
前台和JS
1 <script language="javascript" type="text/javascript">
2 function onClientClick(selectedId,selectedTbId,s1,s2)
3 {
4 //tagname 是一个字符串,代表找到的元素卷标名称
5 var gv;
6 gv=document.getElementById('<%=GridView1.ClientID%>');
7 var inputs = gv.getElementsByTagName("input");
8 var flag=false;
9 for (var i = 0; i < inputs.length; i++) {
10 if (inputs[i].type == "text")
11 {
12 if (inputs[i].id == selectedTbId) {
13 inputs[i].style.display = "none";
14 return false;
15 }
16 }
17 }
18 }
19 </script>
20 <body>
21 <form id="form1" runat="server">
22 <div>
23
24 </div>
25 <asp:GridView ID="GridView1" runat="server"
26 onrowdatabound="GridView1_RowDataBound">
27 <Columns>
28 <asp:TemplateField HeaderText="Info">
29 <ItemTemplate>
30 <asp:TextBox ID="txtInfo" runat="server"></asp:TextBox>
31 <asp:Button ID="btnInfo" runat="server" Text="Info" />
32 </ItemTemplate>
33 </asp:TemplateField>
34 </Columns>
35 </asp:GridView>
36 </form>
37 </body>
38
后台代码
1 protected void Page_Load(object sender, EventArgs e)
2 {
3 if (!IsPostBack)
4 {
5 DataTable dt = new DataTable();
6 dt.Columns.Add("name");
7 dt.Columns.Add("age");
8
9 DataRow dr = dt.NewRow();
10 dr["name"] = "Dolphin";
11 dr["age"] = 18;
12 dt.Rows.Add(dr);
13
14 dr = dt.NewRow();
15 dr["name"] = "Fish";
16 dr["age"] = 19;
17 dt.Rows.Add(dr);
18
19 this.GridView1.DataSource = dt.DefaultView;
20 this.GridView1.DataBind();
21 }
22 }
23 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
24 {
25 if (e.Row.RowType == DataControlRowType.DataRow)
26 {
27 Button rb = (Button)e.Row.FindControl("btnInfo");
28 TextBox tb = (TextBox)e.Row.FindControl("txtInfo");
29 string strName = e.Row.Cells[1].Text.ToString();
30 string strAge = e.Row.Cells[2].Text.ToString();
31
32 if (rb != null)
33 {
34 rb.Attributes.Add("onclick", "return onClientClick('" + rb.ClientID + "','"+tb.ClientID+"','"+strName+"','"+strAge+"')");
35 }
36 }
37 }
相关阅读 更多 +