How to handle form events after adding a master page to an existing page

Here is a question that was asked on my posting trying to invite questions, Got a question- Get an answer here.

Question

Something that would be handy straight from the team is how to handle ASP.NET (2 and later) form events when a Master page is added after the fact.

Say for example a contact page with a button event to submit, but once a Master page is added the contact page lost it's form to the Master page.

Solution

So here is the page before we add a master page to it:

form

formcs

We just have a simple form which handles the click of the submit button.

So then I create a master page and add it by clicking on the MasterPageFile property in Visual Studio and selecting my master page:

addmaster

Now we have the master page added to the ASPX page.  We just have to update the HTML accordingly.  So we want to get rid of all the beginning information, including <html><body><form> tags and just have the tags of our content.  We then place that inside a ContentPlaceHolder tag that looks like:

 

<asp:Content ID="Content2" Runat="Server" 
ContentPlaceHolderID="ContentPlaceHolder1">
...
</asp:Content>

 

We then remove our closing tags for those objects.  You want to make sure your ContentPlaceHolderID matches the placeholder on the master page you are trying to fill.  So my converted page now looks like:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" MasterPageFile="~/Test.master" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div>
    
    <asp:Label ID="Label1" runat="server" Text="Email"></asp:Label>
&nbsp;&nbsp;&nbsp;
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <br />
    <br />
    First Name&nbsp;&nbsp;
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
    <br />
    Last Name&nbsp;&nbsp;
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" οnclick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
    </div>
</asp:Content>

The events themselves are handled just the same, in the originating page.  It is just the form tag that is in the master page.  So that is all we have to do.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值