请问我页面上有一个GridView。我想实现鼠标单击某一条数据,怎样将这条数据填充到页面对应的textbox和DropDownList里去。请问用javascript怎么实现最好是代码~
解决方案 »
- 关于asp.net开发用C#和JS的问题
- 关于GridView 数据绑定的问题,请高手指点````
- asp.net连接access
- 图片在磁盘中,路径在库里,怎样能知道图片的大上(c#)
- 想实现就象动网一样的分类,想了半天也没想好如何递归得到数据,以及绑定到DropDownList
- 如何遍历DataSet的行?
- 请问怎么获得系统的当前用户名?怎么检查某个文件夹下是否存在文件,且只知道文件名的一部分,即模糊查询?
- asp.net运行时页面中所有的中文内容变成乱码?实在没分了,请各位兄弟帮帮忙,真的很急,谢了!
- 我刚开始学习ASP.NET 的问题
- 求Email验证和密码验证的正则表达式
- ASP调用一个“插入数据库的存储过程”,请问如何判断SQL操作正确与否?
- ISAPI_Rewrite如何配置多域名
然后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;
}
}
在DataBound()时间中添加attribute.add()添加事件
//=========================================选择行事件================================================= //参数依次为(后两个如果指定为空值,则不会发生相应的事件):
//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
<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"> </span>
<select id="Select1">
</select>
</div>
</form>
</body>
</html>