How to write Events for user controls in asp.net

Posted by Gayathri under ASP.NET on 9/12/2013 | Points: 10 | Views : 2330 | Status : [Member] | Replies : 8
How to write events for user controls in asp.net


Please donot share any links. i have tried much and nothing is working. kindly share the code directly




Responses

Posted by: Bandi on: 9/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://www.codeproject.com/Articles/163038/Approaches-for-User-Control-Event-Handling
http://codebetter.com/brendantompkins/2004/10/06/easily-raise-events-from-asp-net-ascx-user-controls/
http://odetocode.com/articles/94.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Sample Event for User Control ( Change Page Title by using User Control Event)

EventUserControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="EventUserControl.ascx.cs" Inherits="UserControlEvents.EventUserControl" %>


Page title:
<asp:TextBox runat="server" ID="txtPageTitle" />
<asp:Button runat="server" ID="btnUpdatePageTitle" OnClick="btnUpdatePageTitle_Click" Text="Update" />


EventUserControl.ascx.cs
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UserControlEvents
{
public partial class EventUserControl : System.Web.UI.UserControl
{
private string pageTitle;
public event EventHandler PageTitleUpdated;

protected void btnUpdatePageTitle_Click(object sender, EventArgs e)
{
this.pageTitle = txtPageTitle.Text;
if (PageTitleUpdated != null)
PageTitleUpdated(this, EventArgs.Empty);
}

public string PageTitle
{
get { return pageTitle; }
}

protected void Page_Load(object sender, EventArgs e)
{

}
}
}


RegisterUserControl.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RegisterUserControl.aspx.cs" Inherits="UserControlEvents.RegisterUserControl" %>

<%@ Register TagPrefix="My" TagName="EventUserControl" Src="~/EventUserControl.ascx" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<My:EventUserControl runat="server" ID="MyEventUserControl" OnPageTitleUpdated="MyEventUserControl_PageTitleUpdated" />
</div>
</form>
</body>
</html>



RegisterUserControl.aspx.cs
using System;

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

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

}

protected void MyEventUserControl_PageTitleUpdated(object sender, EventArgs e)
{
this.Title = MyEventUserControl.PageTitle;
}

}
}


Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
You can see the below link:-
http://codebetter.com/brendantompkins/2004/10/06/easily-raise-events-from-asp-net-ascx-user-controls/

Happy Coding.

If it helps you or directs U towards the solution, MARK IT AS ANSWER

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Mahesh,
I have already posted that link too...............

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Allemahesh on: 9/12/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Dear Chandu,

Ok, I have not seen.

Thanks for your replay.

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/12/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Gayathri,
Mark replies as answer if posts help you

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/13/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
yes Bandi post good one.
Hello bandi how to implemented this mvc3 can u post here?



Mark as Answer if its helpful to you

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Hi Jayakumar,
Refer these links once
http://stackoverflow.com/questions/4811732/usercontrol-equivalent-in-mvc3
http://www.primaryobjects.com/CMS/Article129.aspx ( look into the "Passing Parameters to the User Control " section)

how to add a usercontrol in mvc3
You can use partial view in MVC3
@Html.Partial("_Foo")

where in _Foo.ascx you could include server side controls:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<%@ Register Assembly="SomeAssembly" Namespace="SomeNs" TagName="foo" %>

<foo:SomeControl runat="server" ID="fooControl" />

Refernces: http://forums.asp.net/t/1805784.aspx

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Gayathri, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response