Some Asp.net tips

Source: Internet
Author: User
ArticleDirectory

Since I recently started to write some Asp.netProgramSo I am not very familiar with a small skill, so I keep collecting it from the Internet and make a memo myself. Maybe some friends can also use it.

1. The header of the gridview is not displayed. (Blog from yanli0823)

Directly Using the showheader = false attribute does not play any role, and the header should still exist. I tried it in the background. CSCodeGridview1.showheader = false is used in the file. This method is not supported;

Then, use the CSS style and reference the style "display: none" in headerstyle to achieve the desired effect. Code:

<Headerstyle cssclass = "gvhideheader"/>

In the CSS style File

. Gvhideheader

{

Display: none;

}

2. gridview Hyperlinkfield, and make this hyperlink contain parameters. (Source: msdn)

 

headertext = "Order ID"/>
headertext = "Product ID"/>
datatextformatstring = "{0: c} "
datanavigateurlfields =" productid "
datanavigateurlformatstring = "~ \ Details. aspx? Productid = {0} "
headertext =" price "
Target =" _ blank "/>

Headertext = "quantity"/>

3. Two common methods for retrieving row data of the gridview in the background

1). In the backgroundIn the rowcommand event, first obtain the row number and then access the values of each column based on the row number.

In webcontrol, it is not as easy as winform to obtain the row number.

Because the grid does not exist during the Web submission process, the background code executed on the server cannot find the so-called E. rowindex or something.

Using the default buttonfield in the gridview, I found a lot of information and did not know how to obtain the row number. It seems that I want to bind the row number

In its commandarguments, it can be in 2.0, and I didn't find any commandarguments in buttonfield

Therefore, only the template column is used. The following example is used:

The gridview in aspx is as follows:

<Asp: templatefield headertext = "cancel shipping">
<Itemtemplate>
<Asp: button runat = "server" text = "cancel" commandname = "cancelsend"/>
</Itemtemplate>
</ASP: templatefield>

No binding is required. The following row number is read in the background:

C #:

If (E. commandname = "cancelsend ")
{
Gridviewrow DRV = (gridviewrow) (button) (E. commandsource). Parent. Parent ));

// Command source for the event caused by commandsource
Int rowindex = DRV. rowindex;
}

It is strange that the buttonfield of the grid itself cannot be converted.

It is really strange that commandsource is the gridview itself when reading with buttonfield. In this way, I don't know what the buttonfield is.

 

There is another method that can be bound to the command parameters, but it doesn't feel like this method is direct. This is not described here.

 

2 ).PassThe commandargument parameter is passed.FangMethod:

In the Page code, commandargument= '<% # Eval ("ID") + "," + eval ("name") + "," + eval ("Age") %>'

The background value is as follows:Object [] arguments = E.Commandargument. Tostring (). Split (',');

The operation is very simple. Now let's talk about the principle I understand.

CommandargumentIs an object type attribute. To pass multiple parameters, you can only splice strings and then split them in the background.

Fortunately, the Asp.net eval method can easily complete such a function.Commandargument= '<% # Eval ("ID") + "," + eval ("name") + "," + eval ("Age") %>'

The execution process is as follows: bind the eval, and then encapsulate the things in <% # %> In tostring, and then encapsulate them as objects.UploadDelivery.

You can try this method if you don't believe it.Commandargument= '<% # Eval ("ID"), Eval ("Name"), Eval ("Age") %>'

During execution, the "tostring" method does not use the "4" parameter overload. Why?

According to the above principle, if Id = 1, name = kuitar, age = 24. the content in the last method is "1", "kuitar", "25"
Tostring ("1", "kuitar", "25", ''may have another parameter, heha.

The first method is different. After binding, the content in <% # %> is "1, kuitar, 25", because the plus sign plays a role. At this time, tostring should be okay.

 

4. Hyperlinkfield in the gridview transmits more than two parameters (   Quiet apartment in downtown area ,   Wooden house)

1 ). when using data controls in a project, we sometimes need to pass multiple parameters, not necessarily the fields in the data table or the values on the page. although there are many methods to implement, here I will provide a method that I use in the project, the method is very simple.
I am usingHyperlinkfield, andHyperlinkfieldOfDatanavigateurlformatstringThe URL assigned to the property, which is written as follows:
(Hyperlinkfield) gridview1.columns [I]). datanavigateurlformatstring = "...? Id = {} & STR = "+ Str.
I isHyperlinkfieldIn gridview1, STR is a variable.

The code must be before gridview1 binds the method.

2). Many times a link usually has several parameters, such as: http://www.mzwu.com/article.asp? U_id = 1 & id = 1197. How can I add multiple parameters to the hyperlinkfield link? It is very simple. You only need to set multiple fields (separated by commas) in the datanavigateurlfields attribute ), then, use {0}, {1}, and {2} in the datanavigateurlformatstring attribute to represent the field value.

 

5. HowGridviewAdd a check box to achieve the effect: ClickHeaderCheck box on to enable all or all of the buttons to be deselected.


1 ), FirstGridviewAddBind Column. The effect of previewing is as follows:

 


How to add a checkbox for a data item: Write it to the rowdatabound event of the background code:

Event code:

Add a checkbox for the item and view it in the browser:

Add a checkbox for the header. The Code is also written in the rowdatabound () event to implement the Code as the final method of the image. To determine the header, you only need to perform an if judgment layer:

The browsing effect in the browser is as follows:

2. in rowdatabound to write code in the event, we must know that gridview structure, gridview itself is a very large object, with each row in it row is an object. Each cell is an object.

The content in a cell can be divided into two types: one is to bind the displayed text, the other is a control, such as a hyperlink button,CheckboxAnd so on.RowdataboundFind these objects:E. Row. cells []. findcontrol (0)Contains the index number of the control.

Javascript is essential to achieve full checkbox selection. Here we can create a separate JS file and then put the JavaScript code to be written into it, then, import the JS file to our webpage. The Code is as follows:


In this way, the effect of full selection is achieved:


 

 

 


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.