问题:如何用JS通过控件<checkbos>获取GridView中某一行的值。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" PageSize="7"
CellPadding="4" CssClass="css" Width="450px" OnRowDataBound="GridView1_OnRowDataBount" OnPageIndexChanging="GridView1_OnPageIndex">
<PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate><asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="pn" HeaderText="名称" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="st" HeaderText="规格" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="offer" HeaderText="供应商" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="price" HeaderText="单价" >
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:BoundField>
</Columns>
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
<table border="0" cellpadding="0" cellspacing="0" class="style1"
style="font-size: 12px; height: 25px; width: 450px;">
<tr>
<td class="style6">
</td>
<td style="width: 450px">
<asp:Button ID="btncom" runat="server" Text="确定" />
<asp:Button ID="btnclose" runat="server" Text="关闭" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowPaging="true"
BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" PageSize="7"
CellPadding="4" CssClass="css" Width="450px" OnRowDataBound="GridView1_OnRowDataBount" OnPageIndexChanging="GridView1_OnPageIndex">
<PagerSettings FirstPageText="首页" LastPageText="尾页" Mode="NextPreviousFirstLast" NextPageText="下一页" PreviousPageText="上一页" />
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<RowStyle BackColor="White" ForeColor="#003399" />
<Columns>
<asp:TemplateField HeaderText="选择">
<ItemTemplate><asp:CheckBox ID="chk" runat="server" /></ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:TemplateField>
<asp:BoundField DataField="pn" HeaderText="名称" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="st" HeaderText="规格" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="offer" HeaderText="供应商" >
<HeaderStyle Wrap="False" />
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="price" HeaderText="单价" >
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:BoundField>
</Columns>
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
</asp:GridView>
<table border="0" cellpadding="0" cellspacing="0" class="style1"
style="font-size: 12px; height: 25px; width: 450px;">
<tr>
<td class="style6">
</td>
<td style="width: 450px">
<asp:Button ID="btncom" runat="server" Text="确定" />
<asp:Button ID="btnclose" runat="server" Text="关闭" />
</td>
</tr>
</table>
解决方案 »
- Asp.Net JavaScript脚本在IE7/IE8都无法执行
- 企业咋这么落后……
- 大伙能不能给发邮件发送的类,或者提供下思路,我的yahoo邮箱老是收不到邮件
- ASP.NET WEBFORM下水晶报表自定义数据源的问题
- UserControl中的控件能否关闭整个窗口?
- 如何用JScript实现客户端上传文件选择?
- 各位大哥帮我看看这段代码,不知道哪有问题啊?为什么提示"由于符号不匹配或数据溢出以外的其他原因,未能转换数据值。例如,数据在数据存
- 急。arraylist绑下到datagrid控件中,我如何知道要绑定的字段
- 异常处理的问题
- 百分求答,DataTable转成List
- 请教,ASP.NET插入FLASH问题
- datatable合并行问题
CheckBox chk = GridView1.Rows[index].FindControl("chk") as CheckBox;//index为行索引
var cbs = gv.getElementsByTagName("input");
var row = 0;
for(var i=0;i<cbs.length;i++)
{
if(cbs[i].type=="checkbox")
{
if(row == index) //index为行索引
{
//to do...
}
row++;
}
}
问题在哪里呢? GVmain应该指的是GridViewk吧.
for( i=1;i<document.all.GVmain.rows.length;i++)
{
var cb=document.all.GVmain.rows(i).cells(0).children(0);
if(cb.checked)
{
temp0=document.all.GVmain.rows(i).cells(1).innerText;
temp1=document.all.GVmain.rows(i).cells(5).innerText;
temp2=document.all.GVmain.rows(i).cells(6).innerText;
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "CheckRow('"+e.Row.RowIndex+"')");
}
}<asp:TemplateField HeaderText="选择">
<ItemTemplate><asp:CheckBox ID="chk" runat="server"
onclick="CK_click(this);"
/></ItemTemplate>
<HeaderStyle Wrap="False" />
<ItemStyle HorizontalAlign="Center" Wrap="False" />
</asp:TemplateField>添加隐藏控件:<input in="Rowindex">
function CheckRow(i)
{
document.getElementById("Rowindex").value = i;
}
funcation CK_click(obj)
{
if(obj.checked)
{
var gv = document.getElementById("GridView1");
var i = document.getElementById("Rowindex").value;
gv.rows[i].cells[2].innerHTML; //取什么列的值,自已改 CELLS的索引
}
}
var cbs = gv.getElementsByTagName("input");
var row = 0;
for(var i=0;i<cbs.length;i++)
{
if(cbs[i].type=="checkbox" && vb[i].checked)
{
if(row == index) //index为行索引
{
//to do...
}
row++;
}
}
如果不这样,你在JS里面怎么调用后台的数据???在前台页面上,如果你取很多行的值,
要么你就在后台取值,前台输出,
要么你就在前台调用后台的方法,
<asp:CheckBox ID="chk" runat="server" onclick="show(this)" />
js:
function show(e)
{
var aa=e.parentNode.parentNode;获取到这一行。
for(int i=0;i<aa.cells.length;i++)
{
alert(aa.cells[i].innerHTML);
alert(aa.cells[i].innerText); }
}