ASP.NET Call Another Element's DoPostBack Function

Posted by blu on Stack Overflow See other posts from Stack Overflow or by blu
Published on 2010-04-16T20:42:39Z Indexed on 2010/04/16 20:53 UTC
Read the original article Hit count: 335

Filed under:
|

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>

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?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about JavaScript