Handle Multiple Form tag in asp.net page?
My we page contains a user control and inheriting a master page and both
having a button so i have put it in a form tag.I am just trying to do an
event bubbling from master page as well as user control .now i cant load
as for multiple form tags error ..how to do it ?
masterpage
<%@ Master Language="C#" AutoEventWireup="True"
CodeBehind="SampleSite.master.cs" Inherits="EventBubbling.SampleSite" %>
<!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>
<title></title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
<asp:Button ID="Button1" runat="server"
Text="MasterPageButton" />
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
User control
<%@ Control Language="C#" AutoEventWireup="True"
CodeBehind="SampleUserControl.ascx.cs"
Inherits="EventBubbling.Controls.SampleUserControl"
EnableViewState="false" %>
<!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 id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="btnUC" runat="server" OnClick="btnUC_Click"
Text="UserControlButton" />
</div>
</form>
</body>
</html>
User control.cs page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventBubbling.Controls
{
public partial class SampleUserControl : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUC_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
}
Main Page
<%@ Page Language="C#" AutoEventWireup="True"
MasterPageFile="~/SampleSite.Master"
CodeBehind="Default.aspx.cs" Inherits="EventBubbling._Default" %>
<%@ Register Src="~/Controls/SampleUserControl.ascx" TagPrefix="uc"
TagName="SampleUserControl" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<div>
<uc:SampleUserControl ID="UC1" runat="server" />
</div>
</asp:Content>
.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace EventBubbling
{
public partial class _Default : System.Web.UI.Page
{
Button btn = new Button();
protected void Page_Load(object sender, EventArgs e)
{
UC1.buttonClick += new EventHandler(UC1_buttonClick);
btn = this.Master.FindControl("Button1") as Button;
btn.Click += new EventHandler(btn_Click);
}
void btn_Click(object sender, EventArgs e)
{
Response.Write("EventBubbling from MasterPage");
}
void UC1_buttonClick(object sender, EventArgs e)
{
Response.Write("EventBubbling from User control");
}
}
}
My Target is to learn event bubbling but i get some basic error while
doing it ..so posted it .
Wednesday, August 14, 2013
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment