asp.net GridView点击行触发事件

pengshidi 2010-06-02 11:37:05
前台和后台代码。这个是用来经过和点击变色。我想在点击行的时候触发LoadUpData( Business.Load( Convert.ToInt32( e.Row.Cells[ 0 ].Text ) ) );这是我写的查询方法。意思就是每次点击行的时候获取userID来查询东西。请问这个怎么写。现在GridView列为userID、name。如果可以的话顺便告诉怎么影藏userid这列,但是我需要他的值。
script type="text/javascript">
var prevselitem = null;
function selectx(row) {
if (prevselitem != null) {
prevselitem.style.backgroundColor = '';
}
row.style.backgroundColor = '#75BAFF';
prevselitem = row;
}
</script>


protected void GV_DATA_RowDataBound( object sender, GridViewRowEventArgs e )
{
if( e.Row.RowType == DataControlRowType.DataRow )
{
e.Row.Attributes.Add( "onmouseover", "if(this!=prevselitem){this.style.backgroundColor='#Efefef'}" );
e.Row.Attributes.Add( "onmouseout", "if(this!=prevselitem){this.style.backgroundColor=''}" );
e.Row.Attributes.Add( "onclick", e.Row.ClientID.ToString() + ".checked=true;selectx(this)" );
e.Row.Attributes[ "style" ] = "Cursor:hand";
}
}
...全文
1204 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
shepengjun 2010-07-13
  • 打赏
  • 举报
回复
pengshidi 2010-06-02
  • 打赏
  • 举报
回复
不好意思。麻烦了。LoadUpData( Business.Load( Convert.ToInt32( e.Row.Cells[ 0 ].Text ) ) ); 比如我把这个写在按钮click事件里。 但是没有参数啊。在按钮click事件里获取点击行的某列值作为参数。
YISUKU_LEE 2010-06-02
  • 打赏
  • 举报
回复
document.getElementById("btnSearch").click();
这个Btn是隐藏的,JS直接触发了, 你可以把LoadUpData 写在这个按钮的事件里
如果不愿意,直接声明Public方法, 用<% LoadUpData() %> 这种方式执行
YISUKU_LEE 2010-06-02
  • 打赏
  • 举报
回复

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (oldRow.Value != "" && oldRow.Value.Equals(e.Row.ClientID))
{
e.Row.Attributes.Add("style", "background-color:#e6c5fc");
}
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.Attributes.Add("onclick", string.Format("rowStyleChange('{0}','{1}','1')", e.Row.ClientID, e.Row.Cells[0].Text));
}
}
}



思路是 在RowDataBound注册事件,可以通过e 来取得你要的任何GirdView的Cell的值。
然后在Javascript 的function中 触发隐藏按钮的事件。或者触发你的LoadUpData事件(JS 直接触发也可以<% LoadUpData() %> ) 但是 LoadUpData必须是Public
pengshidi 2010-06-02
  • 打赏
  • 举报
回复
楼上的。谢谢。 但是没告诉我该怎么执行那个方法啊?
YISUKU_LEE 2010-06-02
  • 打赏
  • 举报
回复

<script type="text/javascript" language="javascript">

function rowStyleChange(id, value) {

if (document.getElementById("oldRow").value == "") {
document.getElementById("oldStyle").value = document.getElementById(id).runtimeStyle.cssText;
document.getElementById("oldRow").value = id;
document.getElementById(id).runtimeStyle.cssText = "background-color:#e6c5fc";
}
else {
document.getElementById(document.getElementById("oldRow").value).runtimeStyle.cssText = document.getElementById("oldStyle").value;
document.getElementById("oldStyle").value = document.getElementById(id).runtimeStyle.cssText;
document.getElementById("oldRow").value = id;
document.getElementById(id).runtimeStyle.cssText = "background-color:#e6c5fc";
}

document.getElementById("clickId").value = value;
document.getElementById("btnSearch").click();

}
</script>

aspx--->
<input type="hidden" id="oldRow" name="oldRow" runat="server" />
<input type="hidden" id="oldStyle" name="oldStyle" runat="server" />
<input type="hidden" id="clickId" name="clickId" runat="server" />
<asp:Button ID="btnSearch" runat="server" OnClick="btn_Click" />

[code=C#]
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (oldRow.Value != "" && oldRow.Value.Equals(e.Row.ClientID))
{
e.Row.Attributes.Add("style", "background-color:#e6c5fc");
}
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
e.Row.Attributes.Add("onclick", string.Format("rowStyleChange('{0}','{1}','1')", e.Row.ClientID, e.Row.Cells[0].Text));
}
}
}

[/code]
Sdh919 2010-06-02
  • 打赏
  • 举报
回复
那点击后的时间怎么写?
YISUKU_LEE 2010-06-02
  • 打赏
  • 举报
回复
<input type="hidden" id="clickId" name="clickId" runat="server" />
页面不是有clickId 这个隐藏控件么?
LoadUpData( Business.Load( Convert.ToInt32(clickId.Value ) ) ); 不就可以了么。

你要什么值,就在隐藏控件传什么值就可以了啊

62,241

社区成员

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

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

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

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