Among the many server-side controls, the gridview control is not very unfamiliar to everyone. Therefore, I will talk about some of its tips here.
When we need to display data in tables to users without considering the efficiency, the server control gridview is a good choice.
In this table, each column represents a field and each row represents a record. Every row is the data we need to obtain from the database, so we need to consider how to display this data. My habit is to convert each column into a template column and then bind the corresponding field. The advantage of this is thatProgramFor members, the flexibility is enhanced. The Microsoft constraint is too strong, and the flexibility for programmers is too bad. Fortunately, there is a template column.
First, check whether you have a template column. If you do not have a template column, click the gridview, select Add new column, and select the template column,
After obtaining the expected template column, we will edit the template column as shown in Figure
After selecting the template column we want to edit, we will get the following information:
After selecting a template, you can see many templates
However, the most common template we use is templatefield, where programmers are more likely to play freely. Here you can display any form you want as needed, such as lable, Textbox, and linkbutton ., If you have any questions, go to msdn to view help.
The most commonly used template is itemtemplate. I like to write myCode, As shown below
<Asp: templatefield headertext = "sex" sortexpression = "sex">
<Itemtemplate>
<Asp: Label id = "label1" runat = "server" text = '<% # This. returnsex (eval ("sex "). tostring () %> '> </ASP: Label>
</Itemtemplate>
</ASP: templatefield>
Careful friends should find that I used eval () when binding fields, and did not use bind () Because eval data binding is one-way binding, only outputs and does not send back, while BIND () data binding is bidirectional. Of course, the efficiency is definitely different.
If the control has many events, you can trigger the response based on your needs. The most important thing is that the command event contains the commandeventargs class in this event. This class has two attributes: commandname, commandargument can be greatly used. We can pass the data we want to transmit to it. For example, we can obtain the primary key field of the row we are editing, and then perform addition, deletion, modification, and query operations based on the primary key field, this is the most useful in work.
For example, the following code
<Asp: imagebutton id = "imagebutton2" imageurl = "~ /Images/new_del.gif"Runat = "server"Oncommand = "imagebutton2_command" commandname = '<% # eval ("column1") %> 'onclientclick = "Return confirm ('Are you sure! ') "/>
When we add a commandfield column and make showeditbutton = "true", we click the edit link on the page to immediately trigger the rowediting event. If deleteeditbutton = "true ", if you click the edit link, the rowdeleting event will be triggered immediately, which is too cumbersome and can be avoided as much as possible. After all, I personally think that my handwritten code has a higher degree of freedom.
But I personally like the selection button. When we click the select button, two events will be triggered, namely, selectedindexchanging and selectedindexchanged. The previous events, I personally think, appear to be soy sauce, while the latter events are more useful.
In subsequent events, we can
Txtkey. Text = (Label) gvsearch. selectedrow. findcontrol ("lblkey"). Text
In the gridview, we can get a column value for editing a row, which helps us to quickly and easily obtain a value we need. This method is very effective!
We can write code in the rowcreated event to achieve the effect of moving the mouse over a row of color.
The Code is as follows:
Protected void gvsearch_rowcreated (Object sender, gridviewroweventargs E)
{
If (E. Row. rowtype = datacontrolrowtype. datarow)
{
E. Row. Attributes. Add ("onmouseover", "currentcolor = This. style. backgroundcolor; this. style. backgroundcolor = '# ccddee '");
E. Row. Attributes. Add ("onmouseout", "This. style. backgroundcolor = currentcolor ");
}
}
Of course, this efficiency is quite low, and the code will be executed once every line is created, which also leads to low code efficiency!
Thank you for reading this document.Article! My personal skills are limited. If there is anything wrong, please forgive me!