Thirty-one codes commonly used in ASP. NET programs (Article 3)

Source: Internet
Author: User

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

}

22.There are three template columns in the DataGrid that contain textbox: dg_shuliang (Quantity)
Dg_danjian (unit price) dg_jine (amount) is in the 5.6.7 column, respectively. The amount must be calculated automatically when the quantity and unit price are entered, that is, the quantity * unit price = amount, and the time limit must be set
Numeric type. How can I use client scripts 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>

23.Why do we always refresh the row selected at the bottom of the DataGrid, and then scroll to the top, the selected row cannot be seen due to the relationship between the screen.

Page_load
Page. smartnavigation = true

24. modify data in the DataGrid. When you click the edit key, the data appears in the text box. How do I 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 ")
}

}

25. 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 );
}

26. 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;

27. 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 ();

28. 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
}
}

29.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 ();

30.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%

31. 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 );

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.