Asp. NET in the GridView some tricks

Source: Internet
Author: User
Tags month name

Asp. NET in the GridView some tricks

First, the background overrides the automatically populated values in the GridView

We can then modify the values in the event triggering process in the GridView, and these values will overwrite those automatic attributes during the specific run. This allows us to quickly and efficiently use the GridView control in VS, while adding some autonomy and operability.

For example:

Iteminserting Event: This event is triggered before data is inserted into the database.

If the primary key is of type GUID, it can be processed within this event: Generate a GUID data, and assign the value to E. values["Id"].

protected void Listview1_iteminserting (object sender, Listviewinserteventargs e)

{

e.values["Sex" = "male"; Before inserting the database, change the gender to male. No matter what you enter, it will eventually be a male.

e.values["Sex" is the value of the gender field that is about to be inserted.

}

Second, make good use of the hyperlinkfield data row of the GridView control

When using the GridView, we often want to add a column of hyperlinks to navigate to an action page that is related to a column of data in that row. In fact, the HyperLinkField in the GridView provides us with a convenient operation. Note that the Datatextfiele is used to bind the selected field to display in the table. The Datanavigateurlforri is the link address of the item, where the binding entry for the URL value is the value of the bound field in the datanavigateurlied above. The last attribute in the data datatextformatstring we will refer to below.

Iii. datatextformatstring in the data under Edit column in GridView

Datatextformatstring is seldom used, it is very convenient for us to control the format of data items, here are some examples of this property for formatting control:

This section is reproduced to: http://blog.csdn.net/ggj00006/article/details/6583849

tr>

Use to obtain or set the text type information how to display

A as a format character

XX refers to the number of digits or decimal points, if there is no specified decimal point, the majority of the format of the default value of 2, the extra bit will be self-rounding five in.

format character say
C value is shown in currency format.
D The values are shown in the ten-bit format.
E The values are shown in scientific format.
F The values are shown in the fixed science format.
G The values are shown in general format.
N
X
Example:
Example values Format Apply Results
12345.6789 "{0:C}" $12,345.68
-12345.6789 "{0:C}" ($12,345.68)
12345 "{0:d8}" 00012345
12345.6789 "{0:e}" 1234568E+004
12345.6789 "{0:f}" 12345.68
12345.6789 "{0:f0}" 12346
12345.6789 "{0:g}" 12345.6789
123456789 "{0:N4}" 123,456,789.0000

Datatextformatstring= "{0:a}"

Use to get or set the date type information how to show

A for format characters

Example values Format Apply Results
2006/1/20 "{0:d}" 2006/1/20
2006/1/20 "{0:d}" January 20, 2006
2006/1/20 "{0:f}" January 20, 2006 12:00
2006/1/20 "{0:f}" January 20, 2006 12:00:00
2006/1/20 "{0:g}" 2006/1/20 12:00
2006/1/20 "{0:g}" 2006/1/20 12:00:00
2006/1/20 ' {0:m} ' or ' {0:m} ' January 20
2006/1/20 ' {0:r} ' or ' {0:r} ' Fri, Jan 2006 0:00:00 GMT
2006/1/20 "{0:s}" 2006-01-20t00:00:00
2006/1/20 "{0:t}" 12:00
2006/1/20 "{0:t}" 12:00:00
2006/1/20 "{0:u}" 2006-01-20 00:00:00z
2006/1/20 "{0:u}" January 20, 2006 04:00:00
2006/1/20 ' {0:y} ' or ' {0:y} ' January 2006

Datatextformatstring= "{0:xxxx}"

Use to specify how the date type information should be displayed

XXXX is the date of the display format, format characters will be the size of

Format characters Explain
D The date is displayed in numbers, and if the date is only a single digit, 10 digits will not be 0.
Dd The date is displayed in numbers, and if the date is only a single digit, the 10 digits will be 0.
Ddd The day of the week is displayed, and the English language system shows the week.
dddd The day of the week is displayed, and the English system shows the full name of the week.
M The month is displayed in numbers, and if the month has only one digit, 10 digits will not be 0.
Mm The month is displayed in numbers, and if the month has only one digit, 10 digits will be 0.
MMM The month name is displayed, and the English language system will display the month.
MMMM The month name is displayed, and the English system shows the full name of the month.
Yy The year is displayed in two digits, for example, 2006 will show 06.
yyyy The year is displayed in four digits, for example, 2006 will show 2006.
H In numbers, if you have only one digit in a few times, 10 digits will not be 0.
hh The 10-bit number will be 0 if it's only a few decimal hours.
M In numbers, if the minute is only a single digit, 10 digits will not be 0.
Mm In numbers, the 10 digits will be 0 if the minute is only a single digit.
S In numbers, the number of seconds, if the number of seconds is only one digit, 10 digits will not be 0.
Ss In numbers, the number of seconds, if the number of seconds is only a single digit, 10 digits will be 0.
Tttt Show morning or afternoon
Other symbols Specify any symbol to display the symbol, usually the symbol is the time separator,/symbol is the date separator.

Example:
Example values Format Apply Results
2006/1/20 "{0:yyyy year M D Day}" January 20, 2006
2006/1/20 "{0:DDD}" Friday
2006/1/20 "{0:yyyy/m/d}" 2006/1/20

Four,"... "instead of the long string in g Ridview

Sometimes too long in-line display often makes our page layout chaotic, the next method can bein G Ridview in the long line content. Note the use of rowdatabound time.

protected void GridView1_RowDataBound (object sender, GridViewRowEventArgs e)

{

if (E.row.rowtype = = Datacontrolrowtype.datarow)

{

String content = E.row.cells[3]. Text;

E.ROW.CELLS[3]. Text = SubStr (content, 12);

}

}

custom method to intercept items in the GridView that are out of the display

private string SubStr (string str, int len)

{

if (str. Length<=len)

{

return str;

}

string newstr = str. Substring (0, Len);

Newstr + = "...";

return newstr;

}

V. Send value to the GridView query

If you have previously modified the operation data in the GridView in the same way as prompted, you can now use this method to do the work more efficiently. It can add values to the where statement in the GridView, which can come from Cookie,session,url values, control properties, or even Routing and form fields.

How boy Now you can use the GridView control in a duck's way.

Asp. NET in the GridView some tricks

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.