ASP.NET调用另一个元素的DoPostBack功能 [英] ASP.NET Call Another Element's DoPostBack Function

查看:119
本文介绍了ASP.NET调用另一个元素的DoPostBack功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有元素内联呈现一个onclick事件处理程序的ASP.NET控件。我想调用该函数,并把它提升目标控制的服务器端事件处理程序。

I have an ASP.NET control that has an onclick event handler rendered inline on the element. I would like to call that function and have it raise the target control's server side event handler.

<asp:CheckBox ID="Foo" 
    runat="server" 
    AutoPostBack="true" 
    Text="Foo" />

<a href="#" 
    onclick="javascript:setTimeout('__doPostBack(\'Foo\',\'\')', 0)">Test
</a>

我创建的复选​​框,看着场上呈现的功能,然后复制到该锚元素的onclick。

I created the checkbox, looked at the rendered function on the field, and then copied that into the onclick on the anchor element.

锚将提高回传,但对于复选框事件处理函数不是提高。

The anchor will raise a postback, but the event handler for the check box is not raised.

protected override void OnLoad(EventArgs e)
{
    // fires for checkbox
    // fires for anchor (the anchor does cause a postback)
}

void Foo_CheckedChanged(object sender, EventArgs e)
{
    // fires for checkbox
    // does not fire for anchor
}

protected override void OnInit(EventArgs e)
{
    this.Foo.CheckedChanged += new EventHandler(Foo_CheckedChanged);
}

是否有可能做到这一点?

Is it possible to do this?

推荐答案

不要尝试手动确定的JavaScript应该是什么样子的回发。使用适当的API为任务...在这种情况下,这正是GetPostBackEventReference为

Don't try to manually determine what the javascript should look like for a postback. Use the proper API for the task... in this case, this is exactly what GetPostBackEventReference is for.

myControl.Attributes.Add("onclick", "javascript:" + ClientScript.GetPostBackEventReference(targetControl));

这篇关于ASP.NET调用另一个元素的DoPostBack功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆