JavaScript操作服务器控件之Gridview控件
Posted asdyzh
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript操作服务器控件之Gridview控件相关的知识,希望对你有一定的参考价值。
1.javascript脚本如下:
<script language="javascript" type="text/javascript">
function gridviewControl()
{
//获取GridView的客户端元素
var table=document.getElementById("GridView1");
//获取第5列(Test_Column,从0开始)的列标题
var title = table.getElementsByTagName("th")[5].innerText;
//循环访问第Test_Column中的控件
for(var i=1;i<table.rows.length;i++)
{
//获取第一个TextBox
var txt1 = table.rows[i].cells[5].getElementsByTagName("INPUT")[0];
//获取第二个TextBox
var txt2 = table.rows[i].cells[5].getElementsByTagName("INPUT")[1];
//获取 CheckBox
var chkItem = table.rows[i].cells[5].getElementsByTagName("input")[2];
//获取第一个DropDownList
var ddlItem = table.rows[i].cells[5].getElementsByTagName("select")[0];
//获取第二个DropDwonList
var ddlItem2 = table.rows[i].cells[5].getElementsByTagName("select")[1];
//测试结果
alert("Title Name is " + title
+ ", /t/n 第一个TextBox的值 " + txt1.value
+ ", /t/n 第二个TextBox的值 " + txt2.value
+ ", /t/n CheckBox的选项值 " + (chkItem.checked ? ‘true‘ : ‘false‘)
+ ", /t/n 第一个DropDownList的选项值是: " + ddlItem.value
+ ", /t/n 第二个DropDownList的选项值是: " + ddlItem2.value);
}
}
</script>
2.aspx代码如下:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestWeb._Default" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml" >
- <head runat="server">
- <title>Untitled Page</title>
- <mce:script language="javascript" type="text/javascript"><!--
- function gridviewControl()
- {
- //获取GridView的客户端元素
- var table=document.getElementById("GridView1");
- //获取第5列(Test_Column,从0开始)的列标题
- var title = table.getElementsByTagName("th")[5].innerText;
- //循环访问第Test_Column中的控件
- for(var i=1;i<table.rows.length;i++)
- {
- //获取第一个TextBox
- var txt1 = table.rows[i].cells[5].getElementsByTagName("INPUT")[0];
- //获取第二个TextBox
- var txt2 = table.rows[i].cells[5].getElementsByTagName("INPUT")[1];
- //获取 CheckBox
- var chkItem = table.rows[i].cells[5].getElementsByTagName("input")[2];
- //获取第一个DropDownList
- var ddlItem = table.rows[i].cells[5].getElementsByTagName("select")[0];
- //获取第二个DropDwonList
- var ddlItem2 = table.rows[i].cells[5].getElementsByTagName("select")[1];
- //测试结果
- alert("Title Name is " + title
- + ", /t/n 第一个TextBox的值 " + txt1.value
- + ", /t/n 第二个TextBox的值 " + txt2.value
- + ", /t/n CheckBox的选项值 " + (chkItem.checked ? ‘true‘ : ‘false‘)
- + ", /t/n 第一个DropDownList的选项值是: " + ddlItem.value
- + ", /t/n 第二个DropDownList的选项值是: " + ddlItem2.value);
- }
- }
- // --></mce:script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div style="float:left;width:200px;">
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- onrowcreated="GridView1_RowCreated">
- <Columns>
- <asp:BoundField HeaderText="CH1" />
- <asp:BoundField HeaderText="CH2" />
- <asp:BoundField DataField="Column1" HeaderText=" Column1 " />
- <asp:BoundField DataField="Column2" HeaderText=" Column2 " />
- <asp:BoundField DataField="Column3" HeaderText=" Column3 " />
- <asp:TemplateField HeaderText=" Test_Column " >
- <ItemTemplate>
- <asp:TextBox ID="gdvTxtValue" runat="server" Text=‘<%# Bind("txtData1") %>‘></asp:TextBox>
- <br />
- <asp:TextBox ID="gdvTxtItem" runat="server" Text=‘<%# Bind("txtData2") %>‘ BackColor="LightBlue"></asp:TextBox>
- <br />
- <asp:CheckBox ID="gdvchkValue" runat="server" Text=‘<%# Bind("chkData1") %>‘ Enabled=‘<%# Bind("chkEnableData1") %>‘/>
- <br />
- <asp:DropDownList ID="ddlItem" runat="server" >
- <asp:ListItem Selected="True"></asp:ListItem>
- <asp:ListItem Text="I‘m No.11" Value="No.11"></asp:ListItem>
- <asp:ListItem Text="I‘m No.12" Value="No.12"></asp:ListItem>
- </asp:DropDownList>
- <br />
- <asp:DropDownList ID="ddlItem2" runat="server">
- <asp:ListItem Selected="True"></asp:ListItem>
- <asp:ListItem Text="I‘m No.21" Value="No.21"></asp:ListItem>
- <asp:ListItem Text="I‘m No.22" Value="No.22"></asp:ListItem>
- </asp:DropDownList>
- </ItemTemplate>
- </asp:TemplateField>
- </Columns>
- </asp:GridView>
- <%--<asp:SqlDataSource ID="SqlDataSource1" runat="server"
- ConnectionString="<%$ ConnectionStrings:PMConnectionString %>"
- SelectCommand="SELECT * FROM [AP_TRANS]"></asp:SqlDataSource>
- <br /><%--DataSourceID="SqlDataSource1"--%>
- <br />
- </div>
- <div style="position:absolute;left:450px;">
- <asp:DropDownList ID="DropDownList1" runat="server" Visible="false" AutoPostBack="True"
- Height="16px" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
- Width="149px">
- <asp:ListItem></asp:ListItem>
- <asp:ListItem Value="VAlue1">1</asp:ListItem>
- <asp:ListItem Value="value2">2</asp:ListItem>
- <asp:ListItem Value="value3">3</asp:ListItem>
- <asp:ListItem></asp:ListItem>
- </asp:DropDownList>
- <asp:Label ID="Label1" runat="server" Text="Label" Visible="false" Width="200px"></asp:Label>
- <asp:Button ID="Button1" runat="server" Text="Button" Visible="false" OnClientClick="gridviewControl()"/>
- <br />
- <input type="button" id="btnTest" value="Test_Button" onclick="gridviewControl()" />
- </div>
- </form>
- </body>
- </html>
3.aspx.cs文件代码如下:
- using System;
- using System.Collections;
- using System.Configuration;
- using System.Data;
- using System.Linq;
- using System.Web;
- using System.Web.Security;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
- using System.Web.UI.WebControls.WebParts;
- using System.Xml.Linq;
- namespace TestWeb
- {
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- //DataTable
- DataTable Record = new DataTable();
- Record.Columns.Add("Column1", typeof(int));
- Record.Columns.Add("Column2", typeof(string));
- Record.Columns.Add("Column3", typeof(string));
- Record.Columns.Add("txtData1", typeof(string));
- Record.Columns.Add("txtData2", typeof(string));
- Record.Columns.Add("chkData1", typeof(bool));
- Record.Columns.Add("chkEnableData1", typeof(bool));
- for (int i = 0; i < 5; i++)
- {
- Record.Rows.Add();
- Record.Rows[i]["Column1"] = i + 1;
- Record.Rows[i]["Column2"] = "参照列1";
- Record.Rows[i]["Column3"] = "参照列2";
- Record.Rows[i]["txtData1"] = "Data of Box1-" + i.ToString();
- Record.Rows[i]["txtData2"] = "Data of Box2-" + i.ToString();
- Record.Rows[i]["chkData1"] = true;
- Record.Rows[i]["chkEnableData1"] = true;
- }
- GridView1.DataSource = Record;
- GridView1.DataBind();
- }
- protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
- {
- //e.Row.Cells[0].Visible = false;
- e.Row.Cells[0].Style.Add("display", "none");
- e.Row.Cells[1].Style.Add("display", "none");
- }
- protected void l2131l()
- {
- //string onclick =" ScriptName=ReturnThsSearch_0&"
- // + " tableId=&valueCount=0&Imode=0‘, null, ‘left=400, top=100, status=no, toolbar= no, location= no, menubar= no ,height=550,width=670‘);";
- //string onclick =" ScriptName=ReturnSearchVendor_0&"
- //+ " WS_CD=&Imode=1‘, null, ‘left=400, top=100, status=no, toolbar= no, location= no, menubar= no ,height=550,width=660‘);";
- }
- protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- this.Label1.Text = DropDownList1.Text;
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- this.Label1.Text = DropDownList1.SelectedItem.Text + "-" + DropDownList1.SelectedItem.Value;
- for (int i = 0; i < this.DropDownList1.Items.Count; i++)
- {
- string ttt = this.DropDownList1.Items[0].Text + DropDownList1.Items[i].Value;
- }
- }
- }
- }
--------------------- 本文来自 goforwarder 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/yd2005zxq/article/details/5613262?utm_source=copy
以上是关于JavaScript操作服务器控件之Gridview控件的主要内容,如果未能解决你的问题,请参考以下文章
javascript 怎么获取dropdownlist上的值,
ListView.GridViewColumn (*) 宽度