Pass value of the master page in ASP. NET (viewstate Version)

Source: Internet
Author: User
Although the master page function has been available for a long time, it has never been used.
Recently I plan to upgrade my website. upgrade net1.1. net 3.5, in addition to new features such as implicit type, extension method, Lambda expression, and LINQ, the feature page of the master page will certainly be played. So today, I used vs2008 to create a website and tried some basic tasks, data binding, and page value transfer.
This was only the first day. Due to the constant interruptions of external tasks during work, there was very little learning achievement. Besides reading some msdn materials, I also checked the information on the Internet, in a post in csdn, the master page cannot pass values through viewstate, which is actually incorrect. The master page can definitely pass values through viewstate, and the method is very simple.
The code for the master page is as follows:
 

<% @ Master language = "C #" autoeventwireup = "true" codefile = "Main. master. cs" inherits = "Main" masterpagefile = "" %>

<! 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> master page </title>
<Asp: contentplaceholder id = "head" runat = "server">
</ASP: contentplaceholder>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Div style = "border-style: dashed; font-family:, Arial, Helvetica, sans-serif; font-size: XX-large; font-weight: 100; font-style: normal; font-variant: normal; text-transform: Capitalize; color: # ff0000 "> main master page! </Div>
<Br/>
<Div align = "center"
Style = "font-family:, Arial, Helvetica, sans-serif; font-size: Large; font-weight: 400; font-style: italic"> Hi! Welcome to ASP. net3.5! <Br/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "button"/>
</Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">

</ASP: contentplaceholder>
<Div align = "center" style = "border-style: Ridge"> All Rights Reserved. Violators are required! Zhejiang New Energy Technology Co., Ltd. 2008 </div>
</Div>
</Form>
</Body>
</Html>

 

Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. xml. LINQ;

Public partial class main: system. Web. UI. masterpage, imasterdata
{
Public int btncount
{
Get
{
Return this. viewstate ["btncount"] = NULL? 0: Convert. toint32 (this. viewstate ["btncount"]);
}
Set
{
This. viewstate ["btncount"] = value;
}
}
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
This. btncount ++;
}
}

Imasterdata
The interface code is as follows:
/// <Summary>
/// Master page data
/// </Summary>
Public interface imasterdata
{
Int btncount {Get; set ;}
}

Method 1:
On the content page, use:
(Main) This. Page. Master). btncount;

This method is not recommended, and the flexibility is very low. This line of code is dangerous if btncount or main is changed or selected on multiple master pages.

Method 2:
On the content page, use:
(Imasterdata) This. Page. Master). btncount;

It is recommended that you define an interface and call the Members to be called through the interface. For interface programming, I don't need to talk about the advantages.

Method 3:
On the content page, use:
This. Page. master. GetType (). getproperty ("btncount"). getvalue (this. Page. Master, null)

This is a relatively flexible method that cannot be used to determine errors during compilation. You can use this method in special cases.

Method 4:
Override object saveviewstate () and override void loadviewstate (Object savedstate)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.