views:

163

answers:

3

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?

A: 

Why not convert the tag to an asp:linkbutton and then have the one handler for both

protected override void OnInit(EventArgs e)
{
    this.Foo.CheckedChanged += new EventHandler(Foo_CheckedChanged);
    this.Bar.Click+= new EventHandler(Foo_CheckedChanged);
}
Conrad Frix
I have considered this, and am holding on to it as a last resort. Semantically the content should be an input having a corresponding label. The ability the click on the container of the two elements is "UI sugar" and may come and go. I would prefer not to change the structure of the content to allow the click (but will do what needs to be done).
blu
A: 

The client ID of you checkbox might now be "Foo".

Use the following to make sure you use the correct ClientID for your element

<a href="#" 
    onclick="javascript:setTimeout('__doPostBack(\'<%= Foo.ClientID %> \',\'\')', 0)">Test
</a>
Glennular
The javascript is copied and pasted from the html markup generated by asp.net, the client id is correct.
blu
+2  A: 

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("onlick", "javascript:" + ClientScript.GetPostBackEventReference(targetControl));
Bryan
Thanks for the reply. This is a simplified example to illustrate an issue form a larger context. In JQuery I wire up the click event on a dom element that tries to pull the function in the onclick event for a checkbox rendered by asp.net. The code from your answer renders javascript synonymous with what I have about, sans the settimeout portion. It does not resolve the issue. Also, there are few typos in the above code; its "onclick", and GetPostBackEventReference does not have an overload for just a control.
blu
Your controls don't have a "lick" event? What kind of outdated browser are you using? The lick interface is the obvious next step after the "touch" craze.
Bryan
I guess I don't understand what you are really trying to do here then. Calling whatever code is happening in the Checkbox's event handler is absolutely trivial... why is it important that it has to be the checkbox event? Why not just call the same method from an anchor click event?
Bryan