1

First off... I'm a noob to both ASP.net and C#. I'm updating a page that already exists to add a button..

My code..

<div style="padding: 10px;float: left;"><table><tr><td><asp:Button id="DoSomething" Text="Build Patch" runat="server" OnClick="DoSomething_click" /><br /></td></tr></table></div>

This lives in my ascx control... the code behind is...

    protected void DoSomething_click(object sender, System.EventArgs e)
    {
        Response.Write("<script> alert('Hi');</script>");
    }

It gets wired up on the default.aspx page in

<%@ Register Src="~/ui/MyView.ascx"  TagName="MyView" TagPrefix="UC" %>

and is used in a asp:repeater...

<UC:MyView PB=<%# Container.DataItem %> runat="server"></UC:MyView>

The repeater creates the buttons inside a <td>

I supose my question is how do I wire up the onclick for these buttons? I debug and my DoSomething_click method never hits.

Captain Hammer
  • 37
  • 1
  • 11

2 Answers2

1

Basically you need to pass your ButtonClick event from UserControl to your webpage ( that contains this userControl). This is known as Bubble up of events

User control portion:

Define your OnClick event for the Button. However, in this event you will pass this to your .aspx page.

public partial class MyView : System.Web.UI.UserControl
{
    public event EventHandler SomethingButtonClick;

    protected void DoSomething_Click(object sender, EventArgs e)
    {
        //pass the event up to the aspx page. also called bubbling up the event.
        if (this.SomethingButtonClick != null)
            this.SomethingButtonClick(this, e);
    }
}

Your page containg the UserControl:

Set the event handler for SomethingButtonClick event in Page_Init() event as :

protected void Page_Init(object sender, EventArgs e)
{
   MyView1.SomethingButtonClick += new EventHandler(MyView_SomethingButtonClick);
}

Add/Define this MyView_SomethingButtonClick in your page code behind itself.

protected void MyView_SomethingButtonClick(object sender, EventArgs e)
{
    //handle the event 
 Response.Write("<script> alert('Hi');</script>");
}
R.C
  • 10,417
  • 2
  • 35
  • 48
0

First off, try specifying type="text/javascript" inside of your script tag:

<script type="text/javascript">alert('hi');</script>

Assuming that doesn't work:

If I understand correctly, you are trying to link the dynamically created button to the code in the codebehind. This can get very, very weird. I would recommend using a "proxy function" of sorts. Create a javascript function elsewhere on the page that gets called by the button(s)' click event. This javascript function can then initiate a postback from there. I'll see if I can find the example I've used elsewhere.

EDIT: I can't find my original example, but this is a good reference: ASP.NET postback with JavaScript

Please note that the __doPostBack() function has TWO underscores, not one.

Community
  • 1
  • 1
Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • 1
    Maybe I misunderstand, but he's not talking about javascript at all in his question? He's creating a custom control and wants to handle events from controls that live inside that custom control I think... – Steve's a D Aug 09 '13 at 12:50
  • @Steve the first part of my answer is to address his `Response.Write` line, which contains what appears to be javascript, but never is actually specified as such. The second half of my answer uses javascript to address what I suspect is the ACTUAL issue, which is that the postback event is never triggered. – Russell Uhl Aug 09 '13 at 13:20
  • @RusselUhl Ah good catch on the JS - Missed that. I think what he's trying to do is fire events from his custom control and handle those events in his aspx code behind. His event IS getting triggered, its just not getting handled the way he thinks it is. He needs to bubble his event up outside of the custom control.... but I don't think we'll figure out what his **actual** issue is as he posted and ran. – Steve's a D Aug 09 '13 at 13:32
  • How does one bubble up the event? The button is inside a ascx control that contains many other things. – Captain Hammer Aug 09 '13 at 13:55
  • @CaptainHammer http://stackoverflow.com/questions/7880850/how-do-i-make-an-event-in-the-usercontrol-and-have-it-handeled-in-the-main-form/7880901#7880901 – Steve's a D Aug 09 '13 at 14:27
  • I think the issue may be that it is inside of a repeater... also, the user control is defined in the markup for the page, but it does not have a variable associated with it. I'm assuming the control is wrapped up inside of the repeater? – Captain Hammer Aug 12 '13 at 14:51