Asp.net Common Code 1 [reprinted]

Source: Internet
Author: User
1. open a new window and send parameters:

// Transfer parameters:
Response. write ("<script> window. open ('*. aspx? Id = "+ this. DropDownList1.SelectIndex +" & id1 = "++" ') </script> ")
// Receiving parameters:
String a = Request. QueryString ("id ");
String B = Request. QueryString ("id1 ");

2. Add ("onclick", "return confirm ('OK? ')");
Button. attributes. add ("onclick", "if (confirm ('Are you sure? ') {Return true;} else {return false ;}")

3. Delete the selected table record int intEmpID = (int) MyDataGrid. DataKeys [e. Item. ItemIndex];
String deletecmd = "delete from employee where emp_id =" + intempid. tostring ()

4. Delete table record warning private void datagrid_itemcreated (Object sender, datagriditemeventargs E)
{
Switch (E. Item. itemtype)
{
Case listitemtype. item:
Case listitemtype. alternatingitem:
Case listitemtype. edititem:
Tablecell mytablecell;
Mytablecell = E. Item. cells [14];
Linkbutton mydeletebutton;
Mydeletebutton = (linkbutton) mytablecell. controls [0];
Mydeletebutton. Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete this information ');");
Break;
Default:
Break;
}
}

5. Click the link of the table row to the other page. private void grdCustomer_ItemDataBound (object sender, System. Web. UI. WebControls. DataGridItemEventArgs e)
{
// Click Open Table
If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
E. Item. Attributes. Add ("onclick", "window. open ('default. aspx? Id = "+ e. Item. Cells [0]. Text + "');");
}
// Double-click a table to connect to another page
// In the itemDataBind event
If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
{
String OrderItemID = e. item. cells [1]. Text;
 
E. item. Attributes. Add ("ondblclick", "location. href = '.../ShippedGrid. aspx? Id = "+ OrderItemID + "'");
}
// Double-click a table to open a new page
If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
{
String OrderItemID = e. item. cells [1]. Text;
 
E. item. Attributes. Add ("ondblclick", "open ('../ShippedGrid. aspx? Id = "+ OrderItemID + "')");
}
★Note :【? Id =] cannot be 【? Id =]

6. parameters passed in Table hyper-connection columns <asp: HyperLinkColumn Target = "_ blank" headertext = "id" DataTextField = "id" NavigateUrl = "aaa. aspx? Id ='
<% # DataBinder. eval (Container. dataItem, "data field 1") %> '& name =' <% # DataBinder. eval (Container. dataItem, "data field 2") %> '/>

7. if (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
{
E. Item. Attributes. Add ("onclick", "this. style. backgroundColor = '#99cc00 ';
This. style. color = 'buttontext'; this. style. cursor = 'default ';");
}
Written in _ ItemDataBound of the DataGrid
If (e. Item. ItemType = ListItemType. Item | e. Item. ItemType = ListItemType. AlternatingItem)
{
E. Item. Attributes. Add ("onmouseover", "this. style. backgroundColor = '#99cc00 ';
This. style. color = 'buttontext'; this. style. cursor = 'default ';");
E. Item. Attributes. Add ("onmouseout", "this. style. backgroundColor =''; this. style. color = '';");
}

8. About Date Format

Date Format setting
DataFormatString = "{0: yyyy-MM-dd }"
// I think it should be in the itembound event
E. items. cell ["your column"]. text = DateTime. parse (e. items. cell ["your column"]. text. toString ("yyyy-MM-dd "))

9. Get error information and go to the specified page // do not use Response. Redirect, but use Server. Transfer
E. g
// In global. asax
Protected void Application_Error (Object sender, EventArgs e ){
If (server. getlasterror () is httpunhandledexception)
Server. Transfer ("myerrorpage. aspx ");

// The remaining non-httpunhandledexception exceptions are handled by ASP. NET itself and okay will be handled :)
}
// Redirect will cause post-back generation and cause loss of error information. Therefore, the page orientation should be executed directly on the server, in this way, you can get the error information on the error handling page and process it accordingly.

10. Clear cookies

Cookie. Expires = [DateTime];
Response. Cookies ("UserName"). Expires = 0

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.