Home  >  Article  >  Backend Development  >  Introduction to C# Web Applications Classic Notes (Volume 2)

Introduction to C# Web Applications Classic Notes (Volume 2)

Y2J
Y2JOriginal
2017-04-26 10:49:351974browse

Read the 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 id=sa" />  </appSettings>

Several namespaces

When using DataSet, use using system.Data.SqlClient

When When configuring Web.config, use using system.Configuration

This reminds me of when I was learning C language in my freshman year

bool visible 
btnSearch.Text = visible? “New Search” : “Search” ;

This is also pretty good

dsResult.tables[“Users”].rows.count 
Conver.Tonint32(ConfigurationSettings.AppSettings[“Cokuale.number”]);

Ruthless 1: Use Session to save the results and bind them

Session[“Search”] = dsResults; 
dsResults = (DataSet) Session[“Search”];

grdResults.DataBind();
In fact, Session, Application, etc. are stored in object type, so they must be displayed in the end. Type conversion type
By the way, use null to judge whether a string type value is obtained.

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

Get a control on the webForm

ImageButton img = (ImageButton)e.Item.FindControl(“Selectbutton”)

Jump:

Server.Transfer(“Caoxicao.aspx”);

Add js script (Attributes attribute) to the server control

imgShow.Attributes.Add(“onclick”,”document.getElementById(‘tbPrefs&#39;).style.display = ‘block&#39;;”);

Then (Style attribute),

img.Style.Add(“Cursor”,&#39;Pointer&#39;);

Color related:

ColorConvert cv = new ColorConvert(); 
Color selected = Color.Empty; 
Selected = (olor)cv.ConvertFromString(White);

Add Cookie

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 p = new HtmlGenericControl(“p”); 
p.Style.Add(“background-color”,bg);

Use this Classes can represent HTML server control tags that are not directly represented by .NET Framework classes, such as 45a2772a6b6107b401db3c9b82c049c2, e388a4556c0f65e1904146cc1a846bee, 6c04bd5ca3fcae76e30b72ad730ca86d and 240cb830ca84ebaabbd07850110b414d

Return DataSet

Public DataSet Contact() 
{ 
         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 data set in Xml format)

DataSet results = new DataSet(); 
Results.ReadXml(new StringReader(fi.ContactRequest(userid)));

When using WebService, just add the [WebMethod] attribute to the method!

If you add cache, then [WebMethod(CacheDurition=600)]

Instantiate WebService

FriendsService.FriendsInfo fi = new FriendsService.FriendsInfo(); 
String userid; 
Userid = fi.GetUserID(“…”);

Tips!

HyperLink reg = new HyperLink(); 
Reg.ToolTip = “… …”;

Checkout:

System.Web.Security.Forms.Authentication.SignOut(); 
Response.write (Request.ApplicaltionPath);

Trace debugging:

Trace.Write 
Trace.Warn

Exception:

1. Throws exception

Program exception throws

Throw new ***Exception(“…”);

2. Catch exceptions

It must be thrown from a try code block at the beginning. The try code block is used to place all codes that may throw exceptions.

Eg:

Try 
                  { 
                            … … 
                  } 
         Catch(ArgumentNullExeption e) 
                  { 
                            … 
                  }

Unhandled exception web.config setting

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

The above is the detailed content of Introduction to C# Web Applications Classic Notes (Volume 2). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn