Set Trigger To Button Id From A UserControl

Last post 11-05-2007 5:07 AM by Jin-Yu Yin - MSFT. 4 replies.

Sort Posts:

  • Set Trigger To Button Id From A UserControl

    10-29-2007, 11:29 PM
    • Loading...
    • lmf232s
    • Joined on 12-09-2003, 5:01 PM
    • Posts 93

    I have a button in a usercontrol that I was to set as the Id for a trigger in the update panel.

    Is this possible and how do I achieve this? I cant seem to figure out how to expose the button on the .aspx page.

  • Re: Set Trigger To Button Id From A UserControl

    10-30-2007, 6:44 AM
    • Loading...
    • aalokitoaami
    • Joined on 04-25-2007, 11:04 AM
    • Kolkata,India
    • Posts 28

    You have to declare an event in the user control. And in usercontrol .cs you raise the declared event in th onclick event of the user control.
    And in your .aspx page you add the UserControl as Trigger ControlId and the event you have declared in the UserControl as the EventName of the AsyncPOstBackTrigger

    Sample Code:(Test.aspx)
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <%= DateTime.Now.ToString()%>
    </ContentTemplate>
    <Triggers>
    <asp:AsyncPostBackTrigger ControlID="WebUserControl1" EventName="CausePostBack" /> </Triggers>
    </asp:UpdatePanel>
    <uc1:WebUserControl id="WebUserControl1" runat="server">
    </uc1:WebUserControl>

    UserControl Code: (.ascx)
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>This is a test
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />

    (.cs)
    protected void Button1_Click(object sender, EventArgs e)
    {  CausePostBack(sender, e);  }
    public event EventHandler CausePostBack;


    Please Don't forget to click on MarkAsAnswer if this helps you

    If this post was useful to you, please mark it as answer. Thank you!
    aspxdev.blogspot.com
  • Re: Set Trigger To Button Id From A UserControl

    10-30-2007, 10:43 AM
    • Loading...
    • lmf232s
    • Joined on 12-09-2003, 5:01 PM
    • Posts 93

    While this works for a AsyncPostBackTrigger it does not completly solve my problem as I need to use a PostBackTrigger.

    See the user control is a file upload control and you cant upload the file using the AsyncPostBack as it needs to be a PostBack which is why i need to get this to work for a postbacktrigger.

     Any suggestions? 

  • Re: Set Trigger To Button Id From A UserControl

    10-30-2007, 10:49 AM
    • Loading...
    • lmf232s
    • Joined on 12-09-2003, 5:01 PM
    • Posts 93

    Well I guess its really no different. You just make the trigger a postbacktrigger and then set the controlId = to the usercontrol.

     Seems to work so far on a test page. I'll integrate this into my page and see how it goes.

  • Re: Set Trigger To Button Id From A UserControl

    11-05-2007, 5:07 AM
    Answer

    Hi,

    Thank you for your post!

    I think the main issue is that the button is in another UserControl, so the question is "How to Trigger PostBack to an UpdatePanel from a separate UserControl?  ", This link mentioned a workground for it.

    But I think the best solution is to Bubble the Click Event of the button to a custom event of UserControl.then specify the custom event as a Trigger for the update panel.

    See these links:

    1. Can I specify a UserControl custom event as a Trigger for update panel on Parent page?

    Ok,Hope these help you!

    Best Regards

    Sincerely,
    Jin-Yu Yin
    Microsoft Online Community Support
Page 1 of 1 (5 items)