Asp.net 2.0 tips (from cnblogs yukaizhao)

Source: Internet
Author: User

Asp.net 2.0 tips

Original: http://www.cnblogs.com/yukaizhao/archive/2007/04/21/net2_0_tips.html
1. After submitting the page, keep the position of the scroll bar

You can add the maintainscrollpositiononpostback command to the page command.

<% @ Page Language = "C #" maintainscrollpositiononpostback = "true" autoeventwireup = "true" codefile = "..." inherits = "..." %>

2. After loading the page, move the focus to a control. You only need to specify the defaultfocus attribute of the form.

<Form ID = "frm" defaultfocus = "txtusername" runat = "server">
...
</Form>

3. Set the default button of form through the defaultbutton attribute, that is, the button triggered when you press ENTER

<Form ID = "frm" defaultbutton = "btnsubmit" runat = "server">
...
</Form>

4. You can use the $ symbol to easily find the control using the findcontrol method.

<Form ID = "form1" runat = "server" defaultfocus = "formvw $ txtname">
<Div>
<Asp: formview id = "formvw" runat = "server">
<Itemtemplate>
Name:
<Asp: textbox id = "txtname" runat = "server"
TEXT = '<% # eval ("firstname") + "" + eval ("lastname") %>'/>
</Itemtemplate>
</ASP: formview>
</Div>
</Form>

In the preceding example, the defaultfocus attribute of form is used to specify the focus control for page loading. You can use the $ symbol to easily locate txtname.

You can also use the following code to easily find the control

Textbox TB = This. findcontrol ("form1 $ formvw $ txtname") as textbox;
If (TB! = NULL)
{
// Access Textbox Control
}

5. For more information about cross-Page Submission, see the original article.

6. Use a strong type to access members of the masterpage attribute. For more information, see the original article.

7. You can use the validationgroup property of the verification control to specify the group to which the verification control belongs, and specify the verification group to be activated in the validationgroup property of the button.

<Form ID = "form1" runat = "server">
Search Text: <asp: textbox id = "txtsearch" runat = "server"/>
<Asp: requiredfieldvalidator id = "valsearch" runat = "server"
Controltovalidate = "txtsearch" validationgroup = "searchgroup"/>
<Asp: button id = "btnsearch" runat = "server" text = "Search"
Validationgroup = "searchgroup"/>
....
Other controls with validators and buttons defined here
</Form>

1. When developing a web control, if the control must be placed in the form on the server side, you can use the page. verifyrenderinginserverform (Control) method to ensure it.

2. Use the resolveclienturl (string) method of the control class to convert The path like/ABC/AB. aspx is converted to the correct URL path, which is an internal method in. net1.0 and public in 2.0.

3. the button control has two events: onclick and oncommand. After a button is clicked, both events are triggered. The difference between the two is that the latter can accept the commanargs parameter, but the former cannot.

4. When writing a web control, you can use the themable feature to specify whether a property can be specified in the skin file.

5. The verification control of net2.0 has a setfocusonerror attribute. You can specify whether to move the focus to the control to be verified when an error occurs.

6. On the ASPX page, you can use <% $ appsettings: settingkey %> to obtain the value of the specified key in the configuration file appsettings configuration section.

7. when the readonly attribute of the Textbox Control is set to true, if the value of the textbox is changed using the JS script on the client side, the value cannot be changed on the server side after submission, you can use request. form [textbox. clientid] obtain the modified value.

8. in. in net1.0, there is only one HTML input file upload control, and in 2.0 there is a webcontrol: fileupload. When you select a file and upload it, you can use its hasfile attribute to determine it, instead of judging fileupload1.postfile! = NULL & fileupload1.postfile. contentlength> 0.

 

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.