C # getting started with Web Applications

Source: Internet
Author: User

Read settings in Web. config

Conn = new SqlConnection (ConfigurationSettings. etettings ["cnFriends. ConnectString"]);



<Deleetask>

<! -- User application and configured property settings go here. -->

<! -- Example: <add key = "settingName" value = "settingValue"/> -->

<Add key = "cnFriends. ConnectionString" value = "data source = (local) \ NetSdk; initial catalog = FriendsData; user id = sa"/> </appSettings>




Several namespaces

When DataSet is used, using system. Data. SqlClient is used.

When configuring Web. config, use using system. Configuration


This reminds me of the C language I learned in my freshman year.

Bool visible

BtnSearch. Text = visible? "New Search": "Search ";


This is pretty good.

DsResult. tables ["Users"]. rows. count

Conver. Tonint32 (ConfigurationSettings. receivettings ["Cokuale. number"]);


1: Use Session to save the result and bind it

Session ["Search"] = dsResults;

DsResults = (DataSet) Session ["Search"];

GrdResults. DataBind ();
In fact, sessions and applications store the object type. Therefore, the type must be explicitly converted.
By the way, null is used to determine whether a value of the string type is obtained.

Hard enough 2: Select rows from DataTable

DataRow [] rows = dsResults. Tables ["Users"]. Select (filter );

DsResults = dsResults. Clone ();

Foreach (DataRow row in rows)

{

DsResults. Tables ["Tables"]. ImportRow (row );

}


Obtain a control on webForm.

ImageButton img = (ImageButton) e. Item. FindControl ("Selectbutton ")


Jump:

Server. Transfer ("Caoxicao. aspx ");


Add a js script (Attributes attribute) to the server control)

ImgShow. Attributes. Add ("onclick", "document. getElementById ('tbprefs'). style. display = 'block ';");
(Style attribute ),

Img. Style. Add ("Cursor", 'pointer ');


Color:

ColorConvert cv = new ColorConvert ();

Color selected = Color. Empty;

Selected = (olor) cv. ConvertFromString (White );


Add Cookie

Response. Cookies. Add (new HttpCookie ("backColor", r ))


My favorites-User Controls

Using FriendsReunion. Controls;

Protectd override void Oninit (EventArgs e)

{

FriendsFooter _ footer = (FriendsFooter) LoadControl (Request. ApplicationPath + "/Controls/FriendsFooter. aspx ");

SubHeader _ subHeader = new SubHeader ();

}

Page. Contros. AddAt (0, _ footer );

Page. Contros. AddAt (0, _ subHeader );

Base. OnInit (e );

}


Create an Html control instance

HtmlGenericControl div = new HtmlGenericControl ("div ");

Div. Style. Add ("background-color", bg );

This class can be used to mark HTML server controls that are not directly represented by the. NET Framework class, such as <span>, <div>, <body>, and <font>


Return DataSet

Public DataSet Contact ()

{

String SQL = "@ Select * from... ...";

DataSet requests = new DataSet ();

New SqlDtaAdapter (SQL, conn). Fill (requests );

// Return requests. GetXml ();
Return requests;

}

Receiving: (when the returned value is a dataset in Xml format)

DataSet results = new DataSet ();

Results. ReadXml (new StringReader (fi. ContactRequest (userid )));


When using WebService, you only need to add the [WebMethod] feature to the method!

If a cache is added, [WebMethod (CacheDurition = 600)]

Instantiate WebService

FriendsService. FriendsInfo fi = new FriendsService. FriendsInfo ();

String userid;

Userid = fi. GetUserID ("…");


Small Tips!

HyperLink reg = new HyperLink ();

Reg. ToolTip = "… ...";


Check out:

System. Web. Security. Forms. Authentication. SignOut ();

Response. write (Request. ApplicaltionPath );


Tracking debugging:

Trace. Write

Trace. Warn


Exception:

1. Throw an exception

Program exception thrown

Throw new *** Exception ("…");

2. Capture exceptions

It must start with a try code block, which is used to hold all codes that may throw exceptions.

Eg:

Try

{

... ...

}

Catch (ArgumentNullExeption e)

{

...

}



Unhandled exceptions web. config settings

<Custom Errors mode = "on" defaultRedriect = "customerror. aspx";/>

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.