C # ASP.NET User Management How do I handle server-side server?

I am creating a user control. The user control is a div, and I want to add a click event to it.

Edit:

A page with this control should handle this event.

How can i do this?

+6
c # user-controls
source share
4 answers

First you need to define an event on your UserControl that allows the Page that contains the UserControl register and handle the event. For example:

 public partial class MyUserControl : System.Web.UI.UserControl { // Event for page to handle public event EventHandler DivClicked; protected virtual void OnDivClicked(EventArgs e) { if (DivClicked != null) DivClicked(this, e); } protected void Page_Load(object sender, EventArgs e) { // Page Load Code goes here } } 

Note: The next part will be easier if you use a control that supports postback. However, since you want to use a div , I will explain how to get the div to do postbacks. I would like to thank Mathew Nolton for this decision.

So, in the Page_Load UserControl method, you need to add the onClick attribute to call the ASP.NET __doPostback() javascript function, which calls the server for your div . This is what gives the div ability to post back to the server. In addition, to determine which control caused the ClientId , I set the ClientId div along with some characters to distinguish my ClientId from other controls. Remember, when you define a div in a .ascx file, it must have an id assigned and set runat="server" to be available on the server side.

So, adding the onClick attribute to the div , it will be returned to the server at any time after clicking it. That way, when postback calls Page_Load to call, I check to see if it was the div that caused the postback. If so, I raise the UserControl DivClicked event, which is handled by the page.

 public partial class MyUserControl : System.Web.UI.UserControl { // Event Definition and Raising methods ... protected void Page_Load(object sender, EventArgs e) { // @@@@ will denote this is an argument I provided. string arg = "@@@@" + divClickableDiv.ClientID; // Add postback method to onClick divClickableDiv.Attributes.Add("onClick", Page.ClientScript.GetPostBackEventReference(divClickableDiv, arg)); if (IsPostBack) { // Get event arguments for post back. string eventArg = Request["__EVENTARGUMENT"]; // Determine if postback is a custom postback added by me. if (!string.IsNullOrEmpty(eventArg) && eventArg.StartsWith("@@@@")) { // Raise the click event for this UserControl if the div // caused the post back. if (eventArg == arg) OnDivClicked(EventArgs.Empty); } } } } 

That should do it for UserControl , now all you have to do is register for the DivClicked event on the page.

Unfortunately, you will not receive development time support for registering for the event. However, you can still add the code to the .aspx page. On the page where you drop the UserControl , add an attribute to the UserControl called OnXXX , where XXX is the name event. So, for my example above, I would define what my UserControl on the page would look like:

 <uc1:MyUserControl ID="MyUserControl1" runat="server" OnDivClicked="MyUserControl1_DivClicked" /> 

Now you add your handler code to the Page codebehind file (assuming you use codebehind) as follows:

 public partial class MyPage : System.Web.UI.Page { // Called whenever div in MyUserControl is clicked. protected void WebUserControl1_DivClicked(object sender, EventArgs e) { // Handle Div click event here. } } 

That should do it. Hope this post helps you.

+9
source share
 <div runat="server" onserverclick="MyClickHandler"> ... </div> 
+1
source share

The answer to Rocka is close, but not quite right (as it doesn't work for me in it, and I understood why).

The code bits for Rocka user control are beautiful: // Event for page processing public event EventHandler DivClicked;

 protected virtual void OnDivClicked(EventArgs e) { if (DivClicked != null) DivClicked(this, e); } 

When using UserControl, the following will not work:

OnDivClicked is not an event handler, it is a function, so you cannot assign it. not to mention when the function is triggered, the actual variable of the event handler is zero, so it is not going to do anything.

The right way:

This assigned the MyUserControl1_DivClicked handler to DivClicked. Unfortunately, this does not actually work, so in the code on the Page_Load page, put this line instead:

this.MyUserControl1.OnClick + = new EventHandler (MyUserControl1);

Also, as a side note, if you do div beanat = "server", then OnClick is displayed as an event that you could connect to. All you need is the passthru property for the EventHandler in the user div control.

0
source share

In the ItemTemplate element of the USE asp: Panel INSTEAD OF div (the panel is displayed as a div) and set the Visibility property as follows:

 <asp:Panel ID="ImgFeatured" runat="server" CssClass="featured-ribbon"> </asp:Panel> 

Code for

 Panel ImgFeatured = (Panel)e.Row.FindControl("ImgFeatured"); ImgFeatured.Visible = true; 
0
source share

All Articles