请问我页面上有一个GridView。我想实现鼠标单击某一条数据,怎样将这条数据填充到页面对应的textbox和DropDownList里去。请问用javascript怎么实现最好是代码~

解决方案 »

  1.   

    Gridview 中添加   onClick="retval(1)" 
    然后JS 中这样写     function retval(kind)
         { 
            if(kind==1)
            {
                   var table = $("<%=GridView1.ClientID%>");
                      
                   $("<%=edFieldCode.ClientID%>").value= table.rows[mSelectRow].cells[0].innerText;
                   $("<%=edFieldname.ClientID%>").value= table.rows[mSelectRow].cells[1].innerText;
                   $("<%=edord.ClientID%>").value= table.rows[mSelectRow].cells[2].innerText;
                   $("<%=chkifzero.ClientID%>").checked= table.rows[mSelectRow].cells[3].innerText=='Y'?true:false;
                   $("<%=edkind.ClientID%>").value=  table.rows[mSelectRow].cells[4].innerText.replace(/[ ]/g,""); 
                   $("<%=chkifprn.ClientID%>").checked= table.rows[mSelectRow].cells[5].innerText=='Y'?true:false;
                   $("<%=chkifShow.ClientID%>").checked= table.rows[mSelectRow].cells[6].innerText=='Y'?true:false;
                   $("<%=edformula.ClientID%>").value= table.rows[mSelectRow].cells[7].innerText;
             }
         } 
      

  2.   


    在DataBound()时间中添加attribute.add()添加事件
      

  3.   

    给datagrid 每一行加一个onclikc 事件.   然后调用JS
      

  4.   


    //=========================================选择行事件=================================================     //参数依次为(后两个如果指定为空值,则不会发生相应的事件):
        //GridView ID, 正常行背景色,交替行背景色,鼠标指向行背景色,鼠标点击后背景色
        
     var   mSelectRow=0;
     function GridViewColor(GridViewId,edSelectRow, NormalColor, AlterColor, HoverColor, SelectColor,SelectRows)
     {
         //获取所有要控制的行
        if ($(GridViewId)==null)
        {
            return;
        }
        
       var AllRows = $(GridViewId).getElementsByTagName("tr");
            
       //设置每一行的背景色和事件,循环从1开始而非0,可以避开表头那一行
       for(var i=1; i<AllRows.length; i++)
       {
                //设定本行默认的背景色
               AllRows[i].style.background = i%2==0?NormalColor:AlterColor;
               
                //如果指定了鼠标指向的背景色,则添加onmouseover/onmouseout事件
                //处于选中状态的行发生这两个事件时不改变颜色
               if(HoverColor != "")
               {
                    AllRows[i].onmouseover = function()
                    {
                     if(!this.selected) this.style.background = HoverColor;
                     }
                    if(i%2 == 0)
                    {
                      AllRows[i].onmouseout = function()
                      {if(!this.selected) this.style.background = NormalColor;}
                    }
                   else
                   {
                     AllRows[i].onmouseout = function()
                     {if(!this.selected)this.style.background = AlterColor;}
                   }
               }         //如果指定了鼠标点击的背景色,则添加onclick事件
            //在事件响应中修改被点击行的选中状态
             if(SelectColor != "")
             {
                   
                   if (mSelectRow>0 && mSelectRow<AllRows.length )
                   AllRows[mSelectRow].style.background = SelectColor;
                   
                    AllRows[i].onclick = function()
                    {
                        //点击单选,如是多选行,则不要for循环。
                        if (SelectRows=="one" || SelectRows=="One")
                        {
                          for(var j=1; j<AllRows.length; j++)
                          {
                             AllRows[j].style.background = NormalColor;
                           }
                        }
                        this.style.background = this.style.background==SelectColor?HoverColor:SelectColor;
                        this.selected = !this.selected;
                     
                      for(var j=1; j<AllRows.length; j++)
                      {
                         
                         if (AllRows[j].style.background == SelectColor)
                         {
                           mSelectRow=j;  
                           $(edSelectRow).value = j-1;
                         }
                       }
                    }
              }
      }
      
      if (getUrlParam("selectno")!=null && mSelectRow<1)
      {
         
         AllRows[(parseInt(getUrlParam("selectno"))+1)*2-1].style.background = SelectColor;
         
       }                     
    }
    这样调用:
          window.onload = function()
          {
               //GridViewColor("<%=GridView1.ClientID%>","<%=edSelectRow.ClientID %>","#fff","#fff","","#ccffcc","one");
          }  就OK
      

  5.   

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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>无标题页</title>    <script language="javascript">
            function test ()
            {
                var buttons = document .getElementsByName ("mybutton");
                for(i = 0; i < buttons.length; i++)
                {
                    if(buttons[i] == event.srcElement)
                    {
                        var index = i+1;
                        var a = document.getElementById ("GridView1").getElementsByTagName("tr")[index].getElementsByTagName("td")[0].innerText;                    document .getElementById ("txt").value = a;
                        
                        var option = document .createElement ("option");
                        option.innerText = a;
                        document .getElementById ("Select1").appendChild (option);
                    }
                }
                
            }
        </script></head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
                <Columns>
                    <asp:TemplateField HeaderText="ID">
                        <ItemTemplate>
                            <%#Eval("ID") %>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="PID">
                        <ItemTemplate>
                            <%#Eval("PkID")%>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="操作">
                        <ItemTemplate>
                            <input name ="mybutton" type ="button" value ="选择" onclick ="test()" />
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            <br />
            <input type ="text" id="txt" /><span lang="zh-cn">&nbsp;&nbsp;&nbsp; </span>
            <select id="Select1"> 
            </select>
        </div>
        </form>
    </body>
    </html>