C#web Application Introduction Classic Learning Notes II-Basic application

Source: Internet
Author: User
Reading settings in Web.config

Conn = new SqlConnection (configurationsettings.appsettings["cnfriends.connectstring"));



<appSettings>

<!--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"/ > </appSettings>




Several namespaces

When a dataset is used, the using system is used. Data.sqlclient

When web.config is configured, the using system is used. Configuration


This reminds me of a freshman learning C language.

BOOL Visible

Btnsearch.text = visible? "New Search": "Search";


This one's fine.

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

Conver.tonint32 (configurationsettings.appsettings["Cokuale.number"]);


Tough enough. 1: Save results with session and bind

session["Search"] = dsresults;

Dsresults = (DataSet) session["Search"];

Grdresults.databind ();
In fact, session,application is the object type, so the last thing you want to do is explicitly convert the type
By the way, it is null to judge whether the value of the string type is taken.

Tough enough. 2: Select rows from the DataTable

datarow[] rows = dsresults.tables["Users". Select (filter);

Dsresults = Dsresults.clone ();

foreach (DataRow row in rows)

{

dsresults.tables["Tables"]. ImportRow (row);

}


Gets a control on the WebForm

ImageButton img = (ImageButton) e.item.findcontrol ("Selectbutton")


Jump:

Server.Transfer ("caoxicao.aspx");


Server Control Add JS script (Attributes property)

IMGSHOW.ATTRIBUTES.ADD ("onclick", "document.getElementById (' Tbprefs '). style.display = ' block ';");
Again (style attribute),

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


Color correlation:

Colorconvert CV = new Colorconvert ();

Color selected = Color.Empty;

Selected = (olor) CV. ConvertFromString (white);


Add cookies

RESPONSE.COOKIES.ADD (New HttpCookie ("BackColor", R))


My favorite----user control

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);

}


New HTML Control Instance

HtmlGenericControl div = new HtmlGenericControl ("div");

Div. Style.add ("Background-color", BG);

Use this class to represent HTML server control tags that are not directly represented by the. NET Framework classes, such as <span>, <div>, <body>, and <font>


return dataset

Public DataSet Contacts ()

{

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

DataSet requests = new DataSet ();

New Sqldtaadapter (sql,conn). Fill (requests);

return requests. GETXML ();
return requests;

}

Receive: (When the return value is a dataset in XML format)

DataSet results = new DataSet ();

Results.readxml (new StringReader, FI. Contactrequest (userid)));


When using WebService, simply add the [WebMethod] feature to the Method!

If you add a cache, [WebMethod (cachedurition=600)]

Instantiation of WebService

Friendsservice.friendsinfo fi = new Friendsservice.friendsinfo ();

String userid;

Userid = fi. GetUserID ("...");


Little tips!

HyperLink reg = new HyperLink ();

Reg.tooltip = "...";


Check out:

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

Response.Write (Request.applicaltionpath);


Track Debugging:

Trace.Write

Trace.Warn


Abnormal:

1. Throw an exception

Program Exception thrown

Throw new ***exception ("...");

2. Catch exception

Must be thrown from a try code block at the start, and the try block is used to place all the code that might throw an exception.

Eg:

Try

{

... ...

}

Catch (argumentnullexeption e)

{

...

}



Unhandled exception 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.