问题:如何用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">
                    &nbsp;</td>
                <td style="width: 450px">
                    <asp:Button ID="btncom" runat="server" Text="确定" />
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnclose" runat="server" Text="关闭" />
                </td>
            </tr>
        </table>

解决方案 »

  1.   


    CheckBox chk = GridView1.Rows[index].FindControl("chk") as CheckBox;//index为行索引
      

  2.   

    看错了,原来楼主要用js获取var gv = document.getElementById("<%= GridView1.ClientID %>");
    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++;
        }
    }
      

  3.   

    我要的结果是:当勾选中GridView某一行前的<checkbox>时,获取那一行的值,请问怎么写代码?在网上找了一些代码,可测试不出结果
    问题在哪里呢? 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;
                  }
             }
      

  4.   

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            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的索引
    }
    }
      

  5.   

    为 GRIDVIEW 添加 RowDataBound 事件
      

  6.   

    隐藏控件:<input id="Rowindex" runat="server" type="hidden" />
      

  7.   

    var gv = document.getElementById("<%= GridView1.ClientID %>");
    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++;
        }
    }
      

  8.   

    你可以考虑用JSON免得你就得在页面写<form>
    如果不这样,你在JS里面怎么调用后台的数据???在前台页面上,如果你取很多行的值,
    要么你就在后台取值,前台输出,
    要么你就在前台调用后台的方法,
      

  9.   

    这个很简单啊 !
    <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);   }
    }