ASP. net2.0

Source: Internet
Author: User
Tags how to use sql server how to use sql connectionstrings

Configure data sources for ASP. net2.0
ASP. NET 2.0 provides a large number of providers, master pages, Theme/skin, and other new technologies, all of which require the support of a database. ASP. NET 2.0 adopts SQL express by default. In actual development, we use SQL Server most, because we need to make certain configurations.
The following describes how to use SQL Server 2 k/2k5 in ASP. NET 2.0 to support databases:

  1. Run the command aspnet_regsql in the command line environment of visuaol Studio 2005. This tool can be used to create or specify a supported database on the specified SQL Server for ASP. NET 2.0. The default name is aspnetdb.
  2. Next, you need to modify the configuration file by modifying $: Windows \ Microsoft. net \ framework \ v2.0.50727 \ config \ machine. but modify the machine. config file is not a good method. Fortunately, Asp. NET 2.0 provides. config file.
  3. open a web application Program . add the following configuration section to the config file: connectionstrings
    remove name = "localsqlserver" />
    Add name = "localsqlserver" connectionstring = "Server = (local ); truste D_connection = false; user id = sa; Pwd = 8848; database = aspnetdb " providername =" system. data. sqlclient " />
    connectionstrings

    first remove it from the machine with remove. the localsqlsever connection configuration defined in config points to the locally installed SQL Express, and then adds a new localsqlserver database connection to the desired SQL Server database.

  4. Then configure a default provider using localsqlserver data connection in the <system. Web> node, as shown below:<System. Web>
    <Membership defaultprovider = "sqlmembershipprovider">
    <Providers>
    <Add connectionstringname = "localsqlserver" name = "sqlmembershipprovider" type = "system. web. security. sqlmembershipprovider, system. web, version = 2.0.0.0, culture = neutral, publickeytoken = b03f5f7f11d50a3a "/>
    </Providers>
    </Membership>
    </System. Web>

  5. In this way, the configuration file is OK. You can click ASP. Net configuration under the web site menu in vs to test whether the configuration is successful.

VS 2005Web. configFiles are also supportedSmart sensing(Intelliisense:

If your smart perception does not come out, it may be because of the namespace relationship:
<Configuration Xmlns="Http://schemas.microsoft.com/.NetConfiguration/v2.0">

Just change the above sentence:
<Configuration>

Smart sensing is enough.

 

Ado. NET 2.0InConnectionstringbuilder 

Writing a connection string is never a difficult task for all developers, but it is a headache.

Let's see how ADO. NET 2.0 solves this problem.Sqlconnectionstringbuilder connbuilder= NewSqlconnectionstringbuilder ();
Connbuilder. datasource= "Localhost";
Connbuilder. userid= "Someuser";
Connbuilder. Password= "Somepassword";
Sqlconnection C= NewSqlconnection (connbuilder. connectionstring );
C. Open ();

It solves at least the following problems:
1. You don't have to worry about a wide variety of connection strings ..
2. No wrong connection string will be written

Connectionstringbuilder In ADO. NET 2.0
In Asp.net 2.0, viewstate can be forcibly transmitted in segments. The page. maxpagestatefieldlength attribute is used. You can set the maximum number of bytes for each page Status field in viewstate. The format is as follows, which must be set in the web. config file:
<Pages maxpagestatefieldlength = "5"/>
Here, the viewstate is set to no more than 5 bytes. If the actual viewstate exceeds this value, multipart transmission is performed, but the size of each segment cannot exceed the value set in maxpagestatefieldlength, the default value is-1, indicating that segment transmission is not performed.
Connectionstringbuilder In ADO. NET 2.0
1 Protected Void Page_load ( Object Sender, eventargs E)
2 {
3 Postbackoptions options = New Postbackoptions (button1, String . Empty );
4
5 Stringbuilder sb = New Stringbuilder ();
6 If (Button1.causesvalidation && This . Getvalidators (button1.validationgroup). Count > 0 )
7 {
8 Options. clientsubmit = True ;
9 Options. Validate mvalidation = True ;
10 Options. validationgroup = Button1.validationgroup;
11
12 SB. append ( " If (typeof (page_clientvalidate) = 'function ') " );
13 SB. append ( " If (page_clientvalidate (\ "" + Button1.validationgroup + " \ " ) = False) return false; " );
14 }
15 If ( ! String . Isnullorempty (button1.postbackurl ))
16 Options. actionurl = Httputility. urlpathencode (button1.resolveclienturl (button1.postbackurl ));
17
18 SB. append ( " This. Disabled = true; " );
19 SB. append (clientscript. getpostbackeventreference (options ));
20 SB. append ( " ; " );
21 Button1.attributes. Add ( " Onclick " , SB. tostring ());
22 }
Connectionstringbuilder In ADO. NET 2.0
In Asp.net 2.0, the gridview is bound to the sqldatasource control. How can we get the number of records returned by sqldatasource? For example, in the sqldatasource control, how does one return the number of record rows? In. NET 2.0, you can use the onselected event of sqldatasource, and set the affectedrows attribute in the select event sqldatasourcestatuseventargs parameter.CodeAs follows:
Protected void sqldatasourceincluselected (Object sender, sqldatasourcestatuseventargs E)
{
Totalrows. Text = E. affectedrows. tostring ();
}
<Asp: sqldatasource id = "sqldatasource1" runat = "server" connectionstring = "Data Source = (local); initial catalog = northwind; user id = sa; Password = 123456; "providername =" system. data. sqlclient "selectcommand =" select [customerid], [companyName], [contactname] from [MERs] "onselected =" sqldatasource1_selected "> </ASP: sqldatasource>
4. in Asp.net 1.1, JavaScript code may be used to set a control as the default focus button (that is, the default focus is on this control) on the page_load page, in ASP.. NET 2.0. In form code, use
Defaultbutton and The defaultfocus attribute is enough, for example
 <Form ID= "Form1"

Defaultbutton= "Btnsubmit"

Defaultfocus= "Textbox1"

Runat= "Server">
When the page is loaded, the default button is btnsubmit, and the focus is on texbox1 by default.
Connectionstringbuilder In ADO. NET 2.0

In Asp.net 1.1, a pop-up dialog box is usually written in the server code as follows:

Btnclick. Attributes. Add ("onclick", "Return confirm ('Are you sure? ');");

Now in ASP. NET 2.0, you only need to use the client code to pull and add an onclientclick.
<Asp: button id = "btnclick" runat = "server" onclientclick = "Return confirm ('Are you sure? '); "Text =" button "> </ASP: button>
5. Custom page controls,
For example, in ASP. . Net 1. 1, to declare a custom page control,
Generally, you need to add register prefix = ........ this is very troublesome, but in Asp.net 2.0, if you are sure that a page custom control should be
Used in the entire project, you only need to add
<System. Web>
<Pages>

<Controls>

<Add tagprefix = "prefixname" namespace = "namespacename"/>

</Controls>

</Pages>

</System. Web>
Prefixname indicates the control identifier and namespace indicates the namespace.

 
In vs2005, you will find that dataformatstring does not work.

At this time, set htmlencode to false at the same time, and dataformatstring will take effect:

< ASP: boundfield Headertext = "Displaytext" Datafield = "Fieldname" Dataformatstring = "{0: F2 }" Htmlencode = "False" >
< Itemstyle Horizontalalign = "Right" Width = "60px" > </ Itemstyle >
< Headerstyle Horizontalalign = "Right" > </ Headerstyle >
</ ASP: boundfield >

 

Connectionstringbuilder In ADO. NET 2.0

< Connectionstrings >
< Clear />
< Add Name = "Localsqlserver" Connectionstring =
"Server =.; Integrated Security = sspi; database = aspnetdb" />
</ Connectionstrings >

Expand the subnode on the Treeview page:
Set the enableclientscript and populatenodesfromclient attributes of the Treeview to "true", and add a subnode to the node in the ontreenodepopulate Event code.

<ASP: TreeviewID= "Treeview1"Enableclientscript= "True"Populatenodesfromclient= "True"Ontreenodepopulate= "Treeviewappstreenodepopulated"Runat= "Server" >
</ASP: Treeview>

 

Protected   Void Treeviewappstreenodepopulated ( Object Sender, treenodeeventargs E)
{
Treenode currentnode=E. node;

//Add a subnode for currentnode
}

 

In the server code, set the default controls and buttons on the page:
The "form" attribute is added to the page class of Asp.net 2.0. Set page. Form. defaultbutton and defaultfocus to the uniqueid of the default button (input control.

void page_init ( Object sender, eventargs e)
{< br> This . page. form. defaultfocus = textbox1.uniqueid;
This . page. form. defaultbutton = button1.uniqueid;
}< br>

1 \ processing of hidden fields: the DataGrid can set the field to visible = false directly, and obtain the value through cell [X]. Text. The gridview function is invalid. You can use the runtime to set this column to hide. Process rowdatabound events.
protected void gridview1_rowdatabound (Object sender, gridviewroweventargs e)
{< br> E. row. cells [5]. visible = false;
}< br> 2 \ obtain the data of the selected column: The DataGrid can be obtained directly from the selected row, and the same code of the gridview cannot be run. The gridview can be obtained through gridviewrow. Btnaudit is the button in the template column.
gridviewrow grdrow = (gridviewrow) btnaudit. Parent. parent;

string Strid = grdrow. cells [0]. text;
string memberid = grdrow. cells [5]. text;
3 \ confirm before deleting a piece of data. You can use the touch version column and place the button control in the touch version column. One client event onclientclick is displayed, here you can write a confirmation to process JavaScript scripts. example:





Article from: http://www.cnblogs.com/ghd258/archive/2005/11/18/279583.html

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.