Common programming code of ASP. NET (2)

Source: Internet
Author: User

1. bind to DropDownList in 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 ()));

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

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

Normal binding expression:
<% # DataBinder. Eval (Container. DataItem, "ContactName") %>

Text + binding expression:
<Asp: Label id = lblDate runat = "server" Text = '<% # "[" + DataBinder. eval (Container, "DataItem. newsCreatedate ") +"] "%> 'forecolor =" Red "> </asp: Label>

Binding expression with the display format:
<% # DataBinder. Eval (Container, "DataItem. USActiveDate", "{0: yyyy-MM-dd}") %>

Combined with the binding expression and modal box:
<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> ");

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"] = "";
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;
}

// 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 = "-./? % & 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>

Definition:
Public enum UserTypeEnum: byte
{
/// <Summary>
/// Individual user
/// </Summary>
Individual = 0,

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

Usage in code:
UserTypeEnum ut = UserTypeEnum. Corporation;
GDCIC. Entities. UserVO 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 ));

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:

Step 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

Step 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.

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

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.