<asp:GridView ID="gvProClass" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                DataSourceID="ObjectDataSource1" Width="100%" CellPadding="4" ForeColor="#333333">
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#EFF3FB" />
                <Columns>
                    <asp:BoundField DataField="Title" HeaderText="产品类别名" SortExpression="Title">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                    </asp:BoundField>
                    <asp:BoundField DataField="PcImg" HeaderText="产品类别图片" SortExpression="PcImg">
                        <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="30%" />
                    </asp:BoundField>
                    <asp:TemplateField HeaderText="【编 辑】" SortExpression="Id">
                        <ItemStyle HorizontalAlign="Center" Width="14%" />
                        <ItemTemplate>
                            <input id="btnEditor" type="button" value="【编 辑】" class="btn" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <EditRowStyle BackColor="#2461BF" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>我想点击编辑时用javascript将这一行的值赋值给指定的文本框。能不能实现?在不用Ajax的情况下。

解决方案 »

  1.   

    可以的
    以下作参考function onRowCbCheck(cb,rowNewsID) {
        var obj = document.getElementById("txtNewsIds");
        if (cb.checked) {
            obj.value += rowNewsID + ",";
        }
        else {
            obj.value = obj.value.replace(","+rowNewsID + ",", ",");
        }
    }<asp:GridView ID="gvListNews" Width="98%" HorizontalAlign="Center" runat="server"
                        OnRowDataBound="gvListNews_RowDataBound" AutoGenerateColumns="False" 
                        BorderColor="Silver" BorderWidth="1px">
                        <RowStyle Height="25px" />
                        <Columns>
                            <asp:TemplateField HeaderText="序号" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"
                                ItemStyle-Width="30px">
                                <HeaderTemplate>
                                    <asp:CheckBox ID="cbAll" runat="server" onclick="CheckAll(this)" />
                                </HeaderTemplate>
                                <ItemTemplate>
                                    <asp:CheckBox ID="cbRow" runat="server" />
                                    <!--上面CheckBox,看后台RowDataBound-->
                                    <asp:HiddenField ID="txtRowID" runat="server" Value='<%# Eval("NID") %>' />
                                </ItemTemplate>
                                <FooterTemplate></FooterTemplate>
                                <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
                                <ItemStyle HorizontalAlign="Center" Width="30px"></ItemStyle>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            没有数据!
                        </EmptyDataTemplate>
                    </asp:GridView>protected void gvListNews_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                String newsID = gvListNews.DataKeys[e.Row.RowIndex].Value.ToString();            CheckBox cb = (CheckBox)e.Row.FindControl("cbRow");
                cb.Attributes.Add("onclick", "onRowCbCheck(this," + newsID + ")");
            }
        }
      

  2.   

    以下的js代码循环取GridView所有值,当然也可以取某一行的值:
        <script language="javascript" type="text/javascript">
            function getGridViewData() 
            {
                var gv = document.getElementById("<%=GridView1.ClientID%>");
                var rowStr = "";
                for (var i = 1; i < gv.rows.length-1; i++) {
                    var myRow = gv.rows(i);
                    for (var j = 0; j < myRow.cells.length; j++) {
                        rowStr += myRow.cells(j).innerText + "/";
                    }
                    alert(rowStr);
                    rowStr = "";
                }
                return false;
            }
        </script>
      

  3.   

    gridview转换成对是tr,根据tr取
      

  4.   

    如何判断这行值是选中的呢?
    用个CheckBox吗?
      

  5.   

    随手写一个,需要Jquery库
    $("#GridViewID tr").each(function() {
                    var item = $(this)
                    var checkBox = item.find('>td>input:checkbox');
                    if (checkBox) {
                        if (checkBox.is(':checked')) {
                            dosomething;
                        }
                    }
                })
      

  6.   

    给你一段代码,jquery ajax 得到数据,然后显示在gridview中,然后单击了gridview中的一行,alert这行的数据
    AJAXLoadProgressForm.aspx:<script src="JS/jquery-1.4.2.js" type="text/javascript"></script>
        <script type="text/javascript">
            function ShowProgressDiv() {
                var ID = $("input#idtxt").val();
                $.ajax({
                    type: "GET",
                    url: "GetGridViewByConditionForm.aspx",
                    data: "id=" + ID,
                    beforeSend: function() {
                        $("div#ProgressDiv").css("display", "block");
                    },
                    success: function(msg) {
                        $("div#ShowSearchResult").html(msg);
                        var html = $("div#ShowSearchResult div#div1").html();
                        $("div#ShowSearchResult").empty();
                        $("div#ShowSearchResult").html(html);
                        $("div#ShowSearchResult>div>table#gvData tr").not(":first-child").hover(function() {
                            $(this).css("cursor","pointer");
                        }, function() {
                            $(this).removeAttr("style");
                        }).click(function() {
                            var arr = new Array();
                            $(this).children("td").each(function() {
                                arr.push($(this).text());
                            });
                            alert(arr.valueOf());
                        });
                    },
                    complete: function() {
                        $("div#ProgressDiv").css("display", "none");
                    }
                });
            }
        </script>
     <form id="form1" runat="server">
           <div style="cursor:hand">
           <input type="text" id="idtxt" name="idtxt" />
           <input type="button" id="LoadBtn" value="LoadDataGridView" onclick="ShowProgressDiv()" />
        </div>
        <div id="ProgressDiv" style="display:none">
            <img alt="Loading" src="Images/ajax-loader.gif" />Loading......
        </div>
        <div id="ShowSearchResult">
        
        </div>
        </form>
    GetGridViewByConditionForm.aspx: <div id="div1">
            <asp:GridView ID="gvData" runat="server">
            </asp:GridView>
        </div>
     protected void Page_Load(object sender, EventArgs e)
            {
                if (Request["id"] != null)
                {
                    SqlConnection conn=null;
                    SqlCommand cmd = null;
                    SqlDataAdapter adapter = null;
                    try
                    {
                        conn = new SqlConnection();
                        conn.ConnectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
                        conn.Open();
                        cmd = new SqlCommand();
                        cmd.Connection = conn;
                        cmd.CommandType = CommandType.Text;
                        String cmdStr = "select * from dbo.Customers";
                        if (Request["id"].ToString()!=String.Empty)
                        {
                            cmdStr += " where CustomerID= '" + Request["id"].ToString() + "'";
                        }
                        cmd.CommandText = cmdStr;
                        adapter = new SqlDataAdapter(cmd);
                        DataSet ds = new DataSet();
                        adapter.Fill(ds);
                        this.gvData.DataSource = ds;
                        this.gvData.DataBind();
                    }
                    catch
                    {
                        Response.Write("Error happend!");
                        Response.Flush();
                        Response.End();
                    }
                    finally
                    {
                        if (adapter != null)
                        {
                            adapter.Dispose();
                        }
                        if (cmd != null)
                        {
                            cmd.Dispose();
                        }
                        if ((conn != null) && (conn.State == ConnectionState.Open))
                        {
                            conn.Close();
                        }
                    }
                }
            }
      

  7.   

    或者直接看我的博客:
    http://www.cnblogs.com/chenping-987123/archive/2010/08/27/1809877.html
      

  8.   

    看来看去得跳槽了,叫我搞asp,悲剧,我是学asp.net的,搞什么飞机咯