locked
usercontrol c#.net to call an event RRS feed

  • Question

  • User1046245955 posted

    I have a user control that contains a textbox and some buttons.

    How can I tell the text has changed?

    here's my code:

    <%@ Register Src="File.ascx" TagName="uDatePicker" TagPrefix="uc1" %>

    <uc1:uDatePicker ID="dfrom" runat="server" CssClass="mydropdownlist" />

    <asp:TextBox ID="datefrom" runat="server" AutoPostBack="true" autocomplete="off" OnTextChanged="datefrom_TextChanged" CssClass="mydropdownlist"></asp:TextBox>
    <asp:CalendarExtender ID="c_date_from" runat="server" Format="dd-MMM-yyyy" TargetControlID="datefrom" Animated="true">
    </asp:CalendarExtender>

    how can i put the OnTextChanged="datefrom_TextChanged" in my uc1:uDatePicker or in codebehind c# .net

    Thursday, July 18, 2019 11:51 AM

All replies

  • User-2054057000 posted

    How can i put the OnTextChanged="datefrom_TextChanged" in my uc1:uDatePicker or in codebehind c# .net. 

    You could use jQuery change event for this in your .aspx page.

    $(".mydropdownlist").change(function () {
        //...
    });

    Then inside this you can make some AJAX call.

    Refer this tutorial - https://www.yogihosting.com/jquery-change/

    Thursday, July 18, 2019 12:22 PM
  • User288213138 posted

    Hi paminchever,

    According to your description, I made a demo for your reference.

    I called the method on main page using delegate in user control.

    usercontrol:
    
    Usercontrol:
    
    <asp:Button ID=Button1 runat="server" Text="I am Inside User Control" OnClick="btnTest_Click" />
    
            public delegate void OnButtonClick(string strValue);
    
            public event OnButtonClick btnHandler;     
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnTest_Click(object sender, EventArgs e)
            {
                
                if (btnHandler != null)
                {
                    btnHandler(string.Empty);
                }
    
    webform:
    
    <%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>
    <uc1:WebUserControl ID="WebUserControl1" runat="server" />
    
    protected void Page_Load(object sender, EventArgs e)
            {
                
                 WebUserControl1.btnHandler += new WebUserControl.OnButtonClick(WebUserControl1_btnHandler);
            }
            void WebUserControl1_btnHandler(string strValue)
            {
                Response.Write("This is method in main");
            }
    

    The result:

    Best regards,

    Sam

    Friday, July 19, 2019 10:07 AM