Common programming code in ASP. NET (2)

Source: Internet
Author: User

1. dropdownlist bound to datalist

Private void dlistorder_editcommand (Object source, system. Web. UI. webcontrols. datalistcommandeventargs E)
{
// Bind order status
For (INT I = 0; I <(dropdownlist) dlistorder. items [E. Item. itemindex]. findcontrol ("ddlflag"). Items. Count; I ++)
{
If (dropdownlist) dlistorder. items [E. item. itemindex]. findcontrol ("ddlflag ")). items [I]. value = DV. table. rows [0] ["orstatus"]. tostring ())
{
(Dropdownlist) dlistorder. items [E. Item. itemindex]. findcontrol ("ddlflag"). items [I]. Selected = true;
}
}
}

2. Another binding method

// Bind the delivery method
Dataview Shiptype = orderso. getshiptypelist ();
Dropdownlist ddlshiptype = (dropdownlist) dlistorder. items [E. Item. itemindex]. findcontrol ("ddlshiptype ");
Ddlshiptype. datasource = Shiptype;
Ddlshiptype. datatextfield = "stname ";
Ddlshiptype. datavaluefield = "stid ";
Ddlshiptype. databind ();
Ddlshiptype. selectedindex = ddlshiptype. Items. indexof (ddlshiptype. Items. findbyvalue (DV. Table. Rows [0] ["orshiptype"]. tostring ()));

3. When editing a record in the DataGrid nested in datalist, locate its datalist row

Protected void mylist_itemcommand (Object source, system. Web. UI. webcontrols. datagridcommandeventargs E)
{
If (E. Item. itemtype = listitemtype. Item | E. Item. itemtype = listitemtype. alternatingitem)
{
Htmlinputhidden odid = (htmlinputhidden) E. Item. cells [0]. findcontrol ("odid ");

Datalistitem item = (datalistitem) E. Item. Parent. Parent. parent;
Dropdownlist flag = (dropdownlist) item. findcontrol ("ddlflag ");
If (flag. selectedindex = 0)
{
If (E. commandname = "delete ")
{
Int iodid = convert. toint32 (odid. value );
// Method for deleting Order Details
Orderso. deleteorderdetail (iodid );
}
}
Else
{
Gdcic. Framework. Utility. MessageBox (this, "modifyerror", "Confirmed orders cannot be modified !! ");
Return;
}

Dlistorder. edititemindex =-1;
Binddata ();
}
}

4. databinding binding expression:

1) normal binding expression
<% # Databinder. eval (container. dataitem, "contactname") %>

2) text + binding expression
<Asp: Label id = lbldate runat = "server" text = '<% # "[" + databinder. eval (container, "dataitem. newscreatedate ") +"] "%> 'forecolor =" red "> </ASP: Label>

3) a binding expression with a display format
<% # Databinder. eval (container, "dataitem. usactivedate", "{0: yyyy-mm-dd}") %>

4) binding expressions and modal boxes
<A href = '<% # showmodalwin (convert. tostring (databinder. eval (container. dataitem, "pictureimage"), convert. tostring (databinder. eval (container. dataitem, "detailid"), convert. tostring (databinder. eval (container. dataitem, "pictureid") %> '>

The showmodalwin () method in the background code file is defined as follows:
Protected string showmodalwin (string pictureimage, string detailid, string pictureid)
{
Return "window. showmodaldialog (\" MERs/showpictureinfo. aspx? PID = "+ pictureimage +" & did = "+ detailid +" & id = "+ pictureid +" \ ", \" \ ", \" dialogheight: 320px; dialogwidth: 480px; center: Yes; help: No; Status: No; scroll: NO \");";
}

You can also extract the parameters and define them as variables:
Const string windowparamstring = "dialogwidth: 540px; dialogheight: 420px; help: 0; Status: 0; resizeable: 1; scroll: No ";

Page. registerstartupscript ("functionscript", "<script language = 'javascript '> window. showmodaldialog ('edituserservice. aspx? Urid = "+ iurid +" ', '','" + windowparamstring + "') </SCRIPT> ");

 

 

5. Add a new row to an existing Dataset

Int iurid = int. parse (session [globals. user_id_session_name]. tostring ());
Dataset myds = userso. getmapuserservicetoservicelist (iurid );

// Add a mail line
Datarow mydr = myds. Tables [0]. newrow ();
Mydr ["svhref"] = "http: // 211.155.23.77 ";
Mydr ["svname"] = "";
Mydr ["usenddate"] = system. datetime. Now. adddays (1 );
Myds. Tables [0]. Rows. Add (mydr );

Dataview mydv = myds. Tables [0]. defaultview;
If (myds. Tables [0]. Rows. Count! = 0)
{
Mydv. rowfilter = "usenddate> '" + system. datetime. Now + "'"; // not expired
Dguserservice. Visible = true;
Dguserservice. datasource = mydv;

Try
{
Dguserservice. databind ();
}
Catch
{
Dguserservice. currentpageindex = dguserservice. pagecount-1;
Dguserservice. databind ();
}
}
Else
{
Dguserservice. Visible = false;
}

6. Add the client verification control

// Verify the Null Value
<Asp: requiredfieldvalidator id = "valusername" runat = "server" controltovalidate = "txtusername" display = "NONE" errormessage = "Enter the user name !! "> </ASP: requiredfieldvalidator>

// Verify the URL
<Asp: regularexpressionvalidator id = "Rev" runat = "server" errormessage = "invalid company website [http: //] is required. "Display =" NONE "controltovalidate =" txtcpwebsite "validationexpression =" http: // ([\ W-] + \.) + [\ W-] + (/[\ W -. /? % & Amp; =] *)? "> </ASP: regularexpressionvalidator>

// Verify the email address
<Asp: requiredfieldvalidator id = "rfv" runat = "server" controltovalidate = "txtcpemail" display = "NONE" errormessage = "Please input your email !! "> </ASP: requiredfieldvalidator>

// Verify the zip code
<Asp: regularexpressionvalidator id = "rev5" runat = "server" errormessage = "invalid zip code! "Display =" NONE "controltovalidate =" txtcppostcode "validationexpression =" \ D {6} "> </ASP: regularexpressionvalidator>

// Display the error message
<Asp: validationsummary id = "vs" runat = "server" showsummary = "false" showmessagebox = "true"> </ASP: validationsummary>

7. Use of enumeration types

1) Definition:
Public Enum usertypeenum: byte
{
/// <Summary>
/// Individual user
/// </Summary>
Individual = 0,

/// <Summary>
/// Unit user
/// </Summary>
Corporation = 1
}

2) usage in the Code:
Usertypeenum ut = usertypeenum. Corporation;
Gdcic. Entities. uservo = gdcic. facade. userso. getuservo (urid );

Ut = (usertypeenum) enum. parse (usertypeenum. Corporation. GetType (), uservo. urusertype );
If (ut = usertypeenum. Corporation)
{
......
}

// Method for directly obtaining the value it represents
Response. Write ("enumeration value:" + convert. toint32 (usertypeenum. Individual ));

8. the user name must start with a letter and cannot contain Chinese characters.

String susername = txtusername. Text. Trim ();
If (! RegEx. ismatch (susername, "^ [A-Za-Z]. *")
{
Utility. MessageBox (this, "nameformaterror", "the user name must start with a letter and do not use a Chinese name !! ");
Return;
}

9. How to fix IIS ing after you delete and reinstall IIS

Run the aspnet_regiis.exe utility:

1,

  • Click Start, and then click Run ".
  • In the open box, type cmd and press Enter.
  • At the command prompt, type the following command and press Enter:
    "% WinDir % \ Microsoft. NET \ framework \ version \ aspnet_regiis.exe"-I

2. In this path, the version indicates the. NET Framework version installed on the server. When you type this command, you must replace this placeholder with the actual version number.

  • Register aspnet_isapi.dll;
  • Click Start, and then click Run ".
  • In the open text box, type the following and press Enter:
    Regsvr32 % WinDir % \ Microsoft. NET \ framework \ version \ aspnet_isapi.dll

Regsvr32 returns the registration result.

10. How to download an object:

Response. appendheader ("content-disposition", "attachment; filename =" + request. Params ("Link"). tostring)
Response. contenttype = request. Params ("contenttype"). tostring
Response. writefile (strlink)
Response. End ()

You can read the file name from the data and directly specify the path to the server. Therefore, you need to specify the path to save the file on the server when uploading the file.

For this method, you need to redirect to a page, which is responsible for processing file writing. You can choose to write or not to write the contenttype parameter. It is best to write the contenttype if it is clear.

However, the appendheader must indicate that the relative path is used to call writefile to output the file.

11. How to Make the client script take effect when the control is dynamically loaded

Event. srcelement to get the event initiator object. When you want to reference multiple objects, you can use a variable to save the control value.

The method is as follows:
Response. Write ("<input type = \" Hidden \ "id = \" jjj \ "value ="/+ this. clientid + "> ");
Note: The ID number of this control is a fixed value.

12. How to register ASPnet on IIS

Aspnet_regiis-I

13. Server Object

1) server. Transfer: Go to another page, but no information is sent to the browser. When the user refreshes, the previous page will be refreshed.
2) server. Execute: execute another page request and return the original page after execution.

14. Methods for disabling request verification

The request verification process detects potentially dangerous client input values, and the processing of the request has been aborted. This value may indicate attempts to compromise application security, such as cross-site scripting attacks. You can disable request verification by setting validaterequest = false in the page instruction or configuration section. However, in this case, it is strongly recommended that the application explicitly check all input.

For example, <system. Web>
<Pages validaterequest = "false"/>
</System. Web>

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.