还是javascript和GridView

okidasougo 2010-11-18 04:21:28
请问我页面上有一个GridView。

我想实现鼠标单击某一条数据,怎样将这条数据填充到页面对应的textbox和DropDownList里去。

请问用javascript怎么实现。。。最好是代码~
...全文
172 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
ds252743641 2010-11-18
  • 打赏
  • 举报
回复
<%@ 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">    </span>
<select id="Select1">
</select>
</div>
</form>
</body>
</html>
cuipinggui 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 xrongzhen 的回复:]
引用楼主 okidasougo 的回复:
请问我页面上有一个GridView。

我想实现鼠标单击某一条数据,怎样将这条数据填充到页面对应的textbox和DropDownList里去。

请问用javascript怎么实现。。。最好是代码~


在DataBound()时间中添加attribute.add()添加事件
[/Quote]



//=========================================选择行事件=================================================

//参数依次为(后两个如果指定为空值,则不会发生相应的事件):
//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



  • 打赏
  • 举报
回复
给datagrid 每一行加一个onclikc 事件. 然后调用JS
okidasougo 2010-11-18
  • 打赏
  • 举报
回复
人工置顶+1
tangserver 2010-11-18
  • 打赏
  • 举报
回复
js哦头大
okidasougo 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cuipinggui 的回复:]
Gridview 中添加 onClick="retval(1)"
然后JS 中这样写

function retval(kind)
{
if(kind==1)
{
var table = $("<%=GridView1.ClientID%>");

$("<%=edFieldCode.ClientID%>").value= table.rows[……
[/Quote]


mSelectRow没定义
xrongzhen 2010-11-18
  • 打赏
  • 举报
回复
[Quote=引用楼主 okidasougo 的回复:]
请问我页面上有一个GridView。

我想实现鼠标单击某一条数据,怎样将这条数据填充到页面对应的textbox和DropDownList里去。

请问用javascript怎么实现。。。最好是代码~
[/Quote]

在DataBound()时间中添加attribute.add()添加事件
cuipinggui 2010-11-18
  • 打赏
  • 举报
回复
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;
}
}

62,272

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧