How do I pass an "on click" event to the Web Page from a User Control?

Hello,

 

I was wondering if anyone can help me with this.

 I have a button on a User Control.

How do i get the "on click" event to pass between the UC and the web page?

Or, better yet, how do I LEAVE the the On Click event code on the Web Page and just have the button be on the UC?

 

Thanks

 

JD

 

PS:  I'm using .NET 2.0 and C#

0
JJDJ
5/1/2008 12:15:36 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

8 Replies
633 Views

Similar Articles

[PageSpeed] 59
Get it on Google Play
Get it on Apple App Store

 Any reason the onClick event can't be used from the user control?


"Hokey religions and ancient weapons are no match for a good blaster at your side."
0
DarthSwian
5/1/2008 12:22:07 PM

The "On Click" event calls a function "LoadGrid()" that is on the Web Page.

 I am looking to have the User Control Button just fire off the "On click" event on the Web Page.

this way, the On Click event can have different code for each page I use it on, but I can still use the User Control button as the trigger.

 

Additional Note:

There are other fields on the User Control that deal with Date/Time formats.

I want to write code on the User Control that validates the format in those text boxes.

that way I can use this control as a way to pass Start and End dates (in the correct format) without having to write the validation code on every web page.

The Button on the  User Control is just a way for me to fire off the On Click code (on the Web Page) that will then reference the Start and End dates on the control and do whatever the "On Click" function (on the Web Page)  is coded to do.

 

Thanks,

 

JD

0
JJDJ
5/1/2008 12:46:32 PM

 You should be able to do the validation for the dates on the control itself no problem.


"Hokey religions and ancient weapons are no match for a good blaster at your side."
0
DarthSwian
5/1/2008 1:20:26 PM

Yes, I can do the validation of the dates on the control.

The issue is that I want to use the (User Control) Buttons On Click event to run a script on the Web Page.

 Any ideas?

 

JD

0
JJDJ
5/1/2008 1:24:55 PM

 Found some information, but it related mostly to static elements on a user control. Still looking


"Hokey religions and ancient weapons are no match for a good blaster at your side."
0
DarthSwian
5/1/2008 1:48:20 PM

 This may be a step in the right direction: http://www.gridviewguy.com/ArticleDetails.aspx?articleID=349_Creating_User_Controls


"Hokey religions and ancient weapons are no match for a good blaster at your side."
0
DarthSwian
5/1/2008 1:49:58 PM

 OK, I'm VB.net so you can convert this (http://labs.developerfusion.co.uk/convert/vb-to-csharp.aspx)

 

User Control Code Behind

 Public Partial Class test
    Inherits System.Web.UI.UserControl

    Public ReadOnly Property myButton() As Button
        Get
            Return btnUser
        End Get
    End Property
End Class

 

Page with registered control Code Behind

 

 Imports System
Imports System.Web.UI
Imports System.Web.UI.UserControl

Partial Public Class WebForm1
    Inherits System.Web.UI.Page

    Protected WithEvents btn As Button

    Private Sub WebForm1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        btn = TestControl.myButton
    End Sub

    Public Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn.Click
        Response.Write("Yes")
        Response.End()
    End Sub
End Class

 

This worked 

 

C# below

CODEBEHIND FOR USER CONTROL
public partial class test : System.Web.UI.UserControl
{
  
    public Button myButton {
        get { return btnUser; }
    }
}


CODE BEHIND FOR PAGE CONSUMING USER CONTROL
using System;
using System.Web.UI;
using System.Web.UI.UserControl;

public partial class WebForm1 : System.Web.UI.Page
{
  
    protected Button btn;
  
    private void WebForm1_Load(object sender, System.EventArgs e)
    {
        btn = TestControl.myButton;
    }
  
    public void btn_Click(object sender, System.EventArgs e)
    {
        Response.Write("Yes");
        Response.End();
    }
}


"Hokey religions and ancient weapons are no match for a good blaster at your side."
0
DarthSwian
5/1/2008 2:17:47 PM

Hi JJDJ,

You can try the following method.

public partial class Test : System.Web.UI.UserControl

{

public event EventHandler buttonClick;

 protected void Button1_Click(object sender, EventArgs e)
    {
        buttonClick(sender, e);
    }

}

Then you can subscribe the event buttonClick at webpage and display the different information .

public partial class Test: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    UserControlID.buttonClick+=new EventHandler(UserControlID_buttonClick);

}

    protected void UserControlID_buttonClick(object sender, EventArgs e)
    {
        Response.Write("hello,I am jack.");
    }

}

Let me know if I have misunderstood what you mean.
Thanks.
Hope it helps,
Hua Jun


Sincerely,
Hua Jun Li
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hua
5/5/2008 2:45:23 AM
Reply:

Similar Artilces:

Javascript inside "web user control" not run if load "web user control" under ajax UpdatePanel
I have this problem and please if some can help me, I place here a sample code to see, but in my real problem the web user control have a very complicate javascript. I have a web user control that have some javascript inside, let say for example.1 <script type="text/javascript" > 2 function RunMe() 3 { 4 alert("Client id:<%=ClientID%>"); 5 } 6 </script> 7 8 9 <a href="javascript:RunMe()">Run javascript</a> a simple call to a simple javascript, inside the web user control And now I have...

User Control in Master page raises "clicked" event...how to subscribe to this event in "nested" Master page??
I have a custom user control with several LinkButtons that serve as a common menu for all my pages.  Within this user control, I created a delegate and a custom event...and raise a "clicked" event whenever one of the LinkButtons is clicked. I added this user control to a Master page, then subscribed to that "clicked" event in the Page_Load.  The subscribed event handler within this Master page works fine. My problem is...I created a "nested" Master page from the Master page above, and want to subscribe to the original user control...

Sharepoint help! Error msg : "A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe"
hihi, I have set up a testing virtual server on my local machine. I installed win server 2003, wss 2.0, sql server 2000, and visual studio .net 2003. i tried to create a project in C# but when i build it and import the web part (.dwp) into the page, there is this error msg "A Web Part or Web Form Control on this Web Part Page cannot be displayed or imported because it is not registered on this site as safe"  I seached most of the sites and they informed that the problem lies in the safe control in web.config i enter the safe control in web.config but i does not know where to put ...

Updating "Parent" Web Page with Value from Pop-up "Child" Web Page
Hi, all. I'm having a problem updating a cell in a table row on my parent web page with the updated value from my child web page. I am using a script to pass the value back to the parent page (script is executed in child web page button exit event handler). If I put the updated value in a textbox on the parent page, it works fine. However, when I attempt to put the updated value in the table row cell I get an error: table row is not an object or the object is null. NOTE: I am doing this without a postback on the parent page. I tried creating all the rows dynamically in the code beh...

Event is being raised in Web User Control, but Container page never "hears" it
My user control has: Public Class __Login Public Event LoginSuccess(ByVal sender As Object, ByVal e As EventArgs) Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnLogin.Click 'Do some crap RaiseEvent LoginSuccess(Me, e) End Sub End Class My container page has: Protected WithEvents LoginControl As __Login Private Sub LoginControl_LoginSuccess(ByVal sender As Object, ByVal e As System.EventArgs) Handles LoginControl.LoginSuccess 'Do som...

How to enable "standard" events in a web user control
Hi there... I'm a pretty newbie in aps.net programming. When I create a new web user control I do not see any of "standard" events (Init / Load / PreRender) in its property window in the IDE. Is there any possibility to enable those events for web user controls to hook my custom code...?  Thanks a lot for any advice.  Lubos Probably the best way is not to use the events but override the implementation.  If your using Visual studio you will get intelli sense.  Write override space On and you will see them.  be sure to leave the "base."...

To clear "input box" value on click in web user control
 Hi there,I have a input box and I am using as a search box. The value of in put box is "search". When i click on the search box, the value of serach box must bu empty and ready for user type.Is there any way to solve this problem? Do I need to use javascript? Thanks. if you are using the aspnet ajax, there is a control in the control toolkit that will do it for you: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/TextBoxWatermark/TextBoxWatermark.aspxMike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.tel...

Inside a web user control ajaxToolkit:AutoCompleteExtender OnClientItemSelected="function" sends "function is undefined"
This is my code....consider that the code is inside a web user control... on a webform this works correctly <script type="text/javascript">function myItemSelectedHandler(source,eventArgs) {var add = document.getElementById('<%= addressTXT.ClientID %>'); showAddress(add); } </script> <asp:TextBox runat="server" ID="addressTXT" Width="300" /><ajaxToolkit:AutoCompleteExtender runat="server" ID="AutoComplete1"BehaviorID="AutoCompleteEx" TargetControlID ="addressTXT" ...

How to preserve the value of a form field when the user press a "BACK" button on my web page?
Hi all,  I have 3 forms with some text fields and checkboxes. I move from Form1 to Form2, and then to Form3. I use session variables to pass values between forms. Now the problem is, when I am in Form3, when the user clicks a "BACK" button on Form3, he should be taken to Form2. At this stage, the data he entered on Form2 should be there as it was! For this, should I load back the values of each field from the session variables? Or is there any automated method to do this (may be from the viewstate etc.)? Can anyone help me? Thanking you in advance, Tomy Viewstate works onl...

Using Non Runat="server" inputs in user control not submitting in web form.
I have created a user control which contains a single input which is not runat="server". A link on the control allows javascript to create more input fields which are given a unique id. I have put the control in my aspx page and it works fine until i submit the form, when the next page as I expected only shows a single input for the control. However the request.form structure doesnt seem to have the values for any of the inputs. Is this because any input that is not runat="server" will not be passed on form submission? If this is the case how can I write a control that will allow the user ad...

superreview cancelled: [Bug 249231] Use only "web site" and "web page" in Firefox : [Attachment 155345] Same as previous with extraneous changes removed
Jeff Walden (remove +fxhelp to email) <jwalden+fxhelp@mit.edu> has cancelled R.J. Keller <rlk@mozdev.org>'s request for superreview: Bug 249231: Use only "web site" and "web page" in Firefox http://bugzilla.mozilla.org/show_bug.cgi?id=249231 Attachment 155345: Same as previous with extraneous changes removed http://bugzilla.mozilla.org/attachment.cgi?id=155345&action=edit ------- Additional Comments from Jeff Walden (remove +fxhelp to email) <jwalden+fxhelp@mit.edu> Removing review request from the patch that's now prompt-hindered w...

superreview requested: [Bug 249231] Use only "web site" and "web page" in Firefox : [Attachment 155345] Same as previous with extraneous changes removed
R.J. Keller <rlk@mozdev.org> has asked Ben Goodger <bugs@bengoodger.com> for superreview: Bug 249231: Use only "web site" and "web page" in Firefox http://bugzilla.mozilla.org/show_bug.cgi?id=249231 Attachment 155345: Same as previous with extraneous changes removed http://bugzilla.mozilla.org/attachment.cgi?id=155345&action=edit ------- Additional Comments from R.J. Keller <rlk@mozdev.org> r=rlk@trfenv.com for the help content. Ben, can you review this for 1.0rc1? ...

Why don't I see the "use master page" check box when I add a new web form to my web app?
From what I've read I should see this check box whenever I add a new web form but I don't.  Should I add something else instead of a web form or is there a flaw in my web apps install? Woot! I found the answer!  There is an item in the add new item dialog box called web content form! If you use this instead of the web form then all is good and warm fuzzy!...

Web part in MOSS not showing in the "Add Web Parts" web page dialog in the nested sites
hey guys,  I created a web part and it seems to work fine ( I followed  the Sahil's article: http://www.codeguru.com/csharp/.net/net_asp/webforms/article.php/c12293__1/)I added the web part using the page:_layouts/NewDwp.aspx located in the profiles site, which is http://ServerName/sites/MySite/then I asked a coworker to try the web part, he went to his profile, click in Site Actions, Edit page, Add a Web Part and he wasn't able to see it in the list, so he needed to go to Site Settings -> Web Parts -> New -> check the web part and click on Populate Gallery. S...

Web resources about - How do I pass an "on click" event to the Web Page from a User Control? - asp.net.web-forms

Resources last updated: 1/17/2016 6:06:27 PM