ASP. NET 2.0 Server Control and form runat = server Mark !!

Source: Internet
Author: User
Source: Chapter E of Mencius

--------------------
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "exist ist. aspx. cs" enableeventvalidation = "false" inherits = "exist ist" %>
--------------------

This article is an extension of ASP. NET 2.0 application development technology and describes the relationship between ASP. NET 2.0 Server controls and tags.

ASP. NET 2.0 applications Program In development, the content in section 1.5.6 on page 1 is about ASP. NET 2.0 Server Control syntax description, because the book is just a simple introduction, more content is now supplemented as follows:

1. Relationship between ASP. NET 2.0 Server controls and <form runat = Server> </form>

ASP. NET 2.0 Server controls (HTML server controls and web server controls) must be included in the <form runat = Server> </form> tag, which can be set as needed, in most cases, controls that only display the interface and do not need to process events can be placed between <form runat = Server> </form>, for most controls, event processing must be performed on the server side and some return values must be obtained. Therefore, it must be placed between <form runat = Server> </form>.

2. How to Control

When the Server Control performs render, addattributestorender, and so on, it will execute the following sentence:

Page page1 = This. page;
If (page1! = NULL)
{
Page1.verifyrenderinginserverform (this );
}

The page. verifyrenderinginserverform method is to verify whether the server control needs to be included in the <form runat = Server> </form> flag. If not included, the following exception is thrown. For exampleCode:

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<SCRIPT runat = "server">

</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> verifyrenderinginserverform </title>
</Head>
<Body>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Form ID = "form1" runat = "server">
</Form>
</Body>
</Html>

When you browse such a page, an exception occurs:

The control textbox1 of the textbox type must be placed in the form tag with runat = server.
Note: An unhandled exception occurs during the execution of the current Web request. Check the stack trace information for details about the error and the source of the error in the code.

Exception details: system. Web. httpexception: the control textbox1 of the type "textbox" must be placed in the form tag with runat = server.

This is because the Textbox Control calls page1.verifyrenderinginserverform (this); During render. Therefore, if it is not placed between the <form runat = Server> </form> flag, this verification process is not acceptable.

However, we can reload this method in the Code so that the textbox control can be placed outside the tag of <form runat = Server> </form>, for example, the following code:

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<SCRIPT runat = "server">
Public override void verifyrenderinginserverform (Control)
{
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> verifyrenderinginserverform </title>
</Head>
<Body>
<Asp: textbox id = "textbox1" runat = "server"> </ASP: textbox>
<Form ID = "form1" runat = "server">
</Form>
</Body>
</Html>

Browsing such a page will not cause exceptions.

3. After adjusting the display mode, can the page work normally?

On msdn, the page. verifyrenderinginserverform method is described as follows:

If the server controls for sending back or using client scripts are not included in the htmlform Server Control (<form runat = "server">) Tag, they will not work properly. These controls can call this method during rendering to provide clear error information when they are not included in the htmlform control.

Yes, although the following code can be normally displayed, once you click the "Submit" button, the server will not get the entered value and the page cannot be saved.

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<SCRIPT runat = "server">
Public override void verifyrenderinginserverform (Control)
{
}

Protected void button#click (Object sender, eventargs E)
{
Response. Write ("<li> textbox1.text =" + textbox1.text );
Response. Write ("<li> request. Params =" + request. Params [textbox1.uniqueid]);
}

Protected void page_load (Object sender, eventargs E)
{
Response. Write ("<li> textbox1.text =" + textbox1.text );
Response. Write ("<li> request. Params =" + request. Params [textbox1.uniqueid]);
If (! Ispostback)
{
Textbox1.text = "ASP. net2.0 Application Development Technology";
}
}
</SCRIPT>

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> verifyrenderinginserverform </title>
</Head>
<Body>
<Asp: textbox id = "textbox1" runat = "server" width = "600px"> </ASP: textbox>
<Form ID = "form1" runat = "server">
<Asp: button id = "button1" runat = "server" onclick = "button#click"
TEXT = "Submit"/>
</Form>
</Body>
</Html>

Therefore, do not move the server control out of the <form runat = Server> </form> flag.

4. How to forcibly place the server control between the <form runat = Server> </form> labels

Some server controls can not be placed between the labels of <form runat = Server> </form>, such as the label control, however, you can use the following method to forcibly place it between the <form runat = Server> </form> labels:

Protected void labelincluprerender (Object sender, eventargs E)
{
This. verifyrenderinginserverform (label1 );
}

5. What are the benefits?

Sometimes, multiple form forms must be placed on the page (although only one form with <form runat = Server> </form> can be implemented ), placing form controls outside the <form runat = Server> </form> flag makes it easy to use. This is common on previous ASP pages and can be implemented in Aspx. The following page not only makes use of the convenience of the server control, but also escaped the control "textbox1" of the type "textbox" must be placed within the form tag with runat = server. For example:

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<SCRIPT runat = "server">
Protected void button#click (Object sender, eventargs E)
{
Response. Write ("<li> textbox1.text =" + textbox1.text );
Response. Write ("<li> request. Params =" + request. Params [textbox1.uniqueid]);
}

protected void page_load (Object sender, eventargs e)
{< br> keywords. TEXT = "ASP. net2.0 Application Development Technology ";
response. write ("

  • textbox1.text =" + textbox1.text);
    response. write ("
  • request. params = "+ request. params [textbox1.uniqueid]);
    If (! Ispostback)
    {< br> textbox1.text = "ASP. net2.0 Application Development Technology ";
    }< BR >}< br> Public override void verifyrenderinginserverform (Control)
    {< BR >}< br>

  • <HTML xmlns = "http://www.w3.org/1999/xhtml">
    <Head runat = "server">
    <Title> verifyrenderinginserverform </title>
    </Head>
    <Body>
    <Form method = "Post" Action = "searchdoc. aspx">
    Keywords: <asp: textbox id = "keywords" runat = "server"> </ASP: textbox>
    <Asp: button id = "button2" runat = "server" text = "Search"/>
    </Form>
    <Form ID = "form1" runat = "server">
    <Asp: textbox id = "textbox1" runat = "server" width = "600px"> </ASP: textbox>
    <Asp: button id = "button1" runat = "server" onclick = "button#click"
    TEXT = "Submit"/>
    </Form>
    </Body>
    </Html>

    On the searchdoc. ASPX page, use request. Form to obtain the entered keyword.

    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.