Thirteen types of Code 2 commonly used in ASP. NET programs

Source: Internet
Author: User
18. Date formatting

[ASPX page: <% # databinder. eval (container. dataitem, "company_ureg_date") %>

Shown as: 19:44:28

I only want]

<% # Databinder. eval (container. dataitem, "company_ureg_date", "{0: yyyy-m-d}") %>
How should I change it?

[Format date]

It is usually the object (datetime) objectfromdb). tostring ("yyyy-mm-dd ");

[Date verification expression]

A. The correct input format is as follows: [], [10:29:39], []

^ (\ D {2} ([02468] [048]) | ([13579] [26]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (\ D {2} ([02468] [1235679]) | ([13579] [01345789]) [\-\/\ s]? (0? [13578]) | (1 [02]) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (3 [01]) | (0? [469]) | (11) [\-\/\ s]? (0? [1-9]) | ([1-2] [0-9]) | (30) | (0? 2 [\-\/\ s]? (0? [1-9]) | (1 [0-9]) | (2 [0-8]) (\ s (0? [1-9]) | (1 [0-2]) \ :( [0-5] [0-9]) (\ s) | (\ :( [0-5] [0-9]) \ s) ([am | Pm | am | PM] {2, 2 })))? $
B. The following correct input format: [0001-12-31], [9999 09 30], [2002/03/03]

^ \ D {4} [\-\/\ s]? (0 [13578]) | (1 [02]) [\-\/\ s]? ([0-2] [0-9]) | (3 [01]) | (0 [469]) | (11 )) [\-\/\ s]? ([0-2] [0-9]) | (30) | (02 [\-\/\ s]? [0-2] [0-9]) $
[Case-sensitive conversion]

Httputility. htmlencode (string );
Httputility. htmldecode (string)

19. How to set global variables

In global. asax

Application_start () event

Add application [attribute name] = xxx;

Is your global variable

20. How do I connect to the connection generated by hyperlinkcolumn and click Connect to open a new window?

Hyperlinkcolumn has a property target, which can be set to "_ blank" (target = "_ blank ")

[Aspnetmenu] Click the menu item to bring up a new window

Add urltarget = "_ blank" to the menu item of your menudata. xml file, for example:

<? XML version = "1.0" encoding = "gb2312"?>
<Menudata imagesbaseurl = "images/">
<Menugroup>
<Menuitem label = "internal reference information" url = "infomation. aspx">
<Menugroup id = "BBC">
<Menuitem label = "announcement information" url = "infomation. aspx" urltarget = "_ blank" lefticon = "file.gif"/>
<Menuitem label = "" url = "newinfo. aspx" lefticon = "file.gif"/>
......
Upgrade your aspnetmenu to version 1.2.

21. Read the textbox value of the DataGrid Control

Foreach (DataGrid DGI in yourdatagrid. Items)
{
Textbox TB = (textbox) DGI. findcontrol ("yourtextboxid ");
TB. Text ....
}
23. three template columns in the DataGrid contain textbox columns dg_shuliang (Quantity) dg_danjian (unit price) dg_jine (amount) in 5.6.7, respectively, it is required that the amount be calculated automatically when the quantity and unit price are entered: quantity * unit price = amount, and the input time limit must be numeric. how can I use a client script to implement this function?

[Si GUI 〗

<Asp: templatecolumn headertext = "quantity">
<Itemtemplate>
<Asp: textbox id = "shuliang" runat = 'server' text = '<% # databinder. eval (container. dataitem, "dg_shuliang") %>'
Onkeyup = "javascript: Docal ()"
/>

<Asp: regularexpressionvalidator id = "REVS" runat = "server" controltovalidate = "shuliang" errormessage = "must be integer" validationexpression = "^ \ D + $"/>
</Itemtemplate>
</ASP: templatecolumn>

<Asp: templatecolumn headertext = "unit price">
<Itemtemplate>
<Asp: textbox id = "danjian" runat = 'server' text = '<% # databinder. eval (container. dataitem, "dg_danjian") %>'
Onkeyup = "javascript: Docal ()"
/>

<Asp: regularexpressionvalidator id = "revs2" runat = "server" controltovalidate = "danjian" errormessage = "must be numeric" validationexpression = "^ \ D + (\. \ D *)? $ "/>

</Itemtemplate>
</ASP: templatecolumn>

<Asp: templatecolumn headertext = "amount">
<Itemtemplate>
<Asp: textbox id = "jine" runat = 'server' text = '<% # databinder. eval (container. dataitem, "dg_jine") %>'/>
</Itemtemplate>
</ASP: templatecolumn> <script language = "JavaScript">
Function Docal ()
{
VaR E = event. srcelement;
VaR ROW = E. parentnode. parentnode;
VaR txts = row. All. Tags ("input ");
If (! Txts. Length | txts. Length <3)
Return;

VaR q = txts [txts. Length-3]. value;
VaR P = txts [txts. Length-2]. value;

If (isnan (q) | isnan (p ))
Return;

Q = parseint (Q );
P = parsefloat (P );

Txts [txts. Length-1]. value = (Q * P). tofixed (2 );
}
</SCRIPT>

24. Why do I always refresh the lower row selected by the DataGrid and scroll to the top? The selected row cannot be seen due to the relationship between the screen.

Page_load
Page. smartnavigation = true

25. modify data in the DataGrid. When you click the edit key, the data appears in the text box. How to control the size of the text box?

Private void maid (OBJ sender, maid E)
{
For (INT I = 0; I <E. Item. cells. Count-1; I ++)
If (E. Item. itemtype = listitemtype. edittype)
{
E. Item. cells [I]. Attributes. Add ("width", "80px ")
}
}

26. Dialog Box

Private Static string scriptbegin = "<script language = \" javascript \ "> ";
Private Static string scriptend = "</SCRIPT> ";

Public static void confirmmessagebox (string pagetarget, string content)
{
String confirmcontent = "Var retvalue = Window. confirm ('"+ content +"'); "+" If (retvalue) {window. location = '"+ pagetarget + "';}";

Confirmcontent = scriptbegin + confirmcontent + scriptend;

Page parameterpage = (PAGE) system. Web. httpcontext. Current. Handler;
Parameterpage. registerstartupscript ("Confirm", confirmcontent );
// Response. Write (strscript );
}

27. format the time: String AA = datetime. Now. tostring ("mm DD, YYYY ");

1.1 take the current year, month, day, hour, minute, second

Currenttime = system. datetime. now;
1.2 get current year

Int year = datetime. Now. Year;
1.3 take the current month

Int month = datetime. Now. month;
1.4 get the current day

Int day = datetime. Now. Day;
1.5 current time

Int = datetime. Now. hour;
1.6 get the current score

Int = datetime. Now. minute;
1.7 takes the current second

Int second = datetime. Now. Second;
1.8 takes the current millisecond

Int millisecond = datetime. Now. millisecond;

28. Custom PagingCode:

First define the variable:

Public static int pagecount; // the total number of pages.
Public static int curpageindex = 1; // current page
Next page:

If (maid-1 ))
{
Datagrid1.currentpageindex + = 1;
Curpageindex + = 1;
}

BIND (); // data binding function
Previous Page:

If (maid> 0)
{
Datagrid1.currentpageindex + = 1;
Curpageindex-= 1;
}

BIND (); // data binding function
Direct page Jump:

Int A = int. parse (jumppage. value. Trim (); // jumppage. value. Trim () indicates the jump value.

If (A <datagrid1.pagecount)
{
This. Maid =;
}

BIND ();

29. Use the DataGrid:

Confirm deletion:

Private void maid (Object sender, system. Web. UI. webcontrols. datagriditemeventargs E)
{
Foreach (maid di in this. Maid)
{
If (Di. itemtype = listitemtype. Item | Di. itemtype = listitemtype. alternatingitem)
{
(Linkbutton) Di. cells [8]. controls [0]). Attributes. Add ("onclick", "Return confirm ('Are you sure you want to delete this item? ');");
}
}
}
Style alternation:

Listitemtype itemtype = E. Item. itemtype;

If (itemtype = listitemtype. item)
{
E. Item. attributes ["onmouseout"] = "javascript: This. style. backgroundcolor = '# ffff ';";
E. Item. attributes ["onmouseover"] = "javascript: This. style. backgroundcolor = '# d9ece1'; cursor = 'hand ';";
}
Else if (itemtype = listitemtype. alternatingitem)
{
E. Item. attributes ["onmouseout"] = "javascript: This. style. backgroundcolor = '# a0d7c4 ';";
E. Item. attributes ["onmouseover"] = "javascript: This. style. backgroundcolor = '# d9ece1'; cursor = 'hand ';";
}
Add an ID column:

Datatable dt = C. executertntableforaccess (sqltxt); // execute the datatable returned by the SQL statement
Datacolumn Dc = DT. Columns. Add ("Number", system. type. GetType ("system. String "));

For (INT I = 0; I <DT. Rows. Count; I ++)
{
DT. Rows [I] ["Number"] = (I + 1). tostring ();
}

Datagrid1.datasource = DT;
Datagrid1.databind ();
Add a checkbox in datagrid1, and add a select-All box to the page.

Private void checkbox2_checkedchanged (Object sender, system. eventargs E)
{
Foreach (maid)
{
(Checkbox) thisitem. cells [0]. controls [1]). Checked = checkbox2.checked;
}
}
Delete all data displayed on the current page.

Foreach (maid)
{
If (checkbox) thisitem. cells [0]. controls [1]). Checked)
{
String strloginid = maid [thisitem. itemindex]. tostring ();
Del (strloginid); // delete a function
}
}

30. When the file is in different directories, you need to obtain the database connection string (if the connection string is placed in Web. config and then initialized in global. asax)

Add the following code to application_start:

Application ["connstr"] = This. Context. Request. physicalapplicationpath + configurationsettings.
Appsettings ["connstr"]. tostring ();

31. Variable. tostring ()

Convert string to string

12345. tostring ("N"); // generate 12,345.00
12345. tostring ("C"); // generate $12,345.00
12345. tostring ("e"); // generate 1.234500e + 004
12345. tostring ("F4"); // generate 12345.0000
12345. tostring ("X"); // generate 3039 (hexadecimal)
12345. tostring ("p"); // generate 1,234,500.00%

32. Variable. substring (parameter 1, parameter 2 );

Part of the string to be truncated. Parameter 1 is the start number of digits on the left, and parameter 2 is the number of digits to be truncated. For example, string S1 = Str. substring );


33. log on to another website on your website: (if your page is nested, because one page can only have one form, you can direct the page to another and submit the login information)

<Script language = "JavaScript">
<! --
Function gook (PWS)
{
FRM. Submit ();
}
// -->

</SCRIPT> <body leftmargin = "0" topmargin = "0" onLoad = "javascript: Gook ()" marginwidth = "0" marginheight = "0">
<Form name = "frm" Action = "http: // 220.194.55.68: 6080/login. php? Retid = 7259 "method =" Post ">
<Tr>
<TD>
<Input id = "f_user" type = "hidden" size = "1" name = "f_user" runat = "server">
<Input id = "f_domain" type = "hidden" size = "1" name = "f_domain" runat = "server">
<Input class = "box" id = "f_pass" type = "hidden" size = "1" name = "pwshow" runat = "server">

<Input id = "LNG" type = "hidden" maxlength = "20" size = "1" value = "5" name = "LNG">
<Input id = "TEM" type = "hidden" size = "1" value = "2" name = "TEM">

</TD>

</Tr>

</Form>
The text box name must be the name on the Web page you want to log on to. If the source code is not working, you can use vsniffer to check it.

The following code obtains the login information entered by the user:

String name;
Name = request. querystring ["emailname"];

Try
{
Int A = Name. indexof ("@", 0, name. Length );
F_user.value = Name. substring (0, );
F_domain.value = Name. substring (a + 1, name. Length-(A + 1 ));
F_pass.value = request. querystring ["psw"];
}

Catch
{
Script. Alert ("wrong email! ");
Server. Transfer ("index. aspx ");
}

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.