Recently wrote a few ASP.NET projects. Among them, ASP.NET's control DataGrid is using the most, and a function is needed in the past few days. I have studied a long time, I found a method, it should be the simplest implementation. It is now shown below.
DataGrid's HTML definition is simple, as follows
<
ASP: DATAGRID
Id
= "MyDataGrid"
Runat
= "Server"
Datakeyfield
= "FID"
AutogenerateColumns
= "False"
>
<
Columns
>
<
ASP: TemplateColumn
ItemStyle-Width
= ".."
.
>
<
ItemTemplate
>
.
ItemTemplate
>
ASP: TemplateColumn
>
Columns
>
ASP: DATAGRID
>
The requirements of the function are in ItemTemplate, it is possible to be a binding data source:
Link code, it is also possible to be one
... span>
The text code is displayed, and the judgment is based on the content of a particular column of each row in the DataTable of the data source binding to the DataGrid.
To put it bluntly, the function is to determine the contents of the data source DataTable every line of DataRow in DataRo, which is the BOOL type. If true, this column shows ... Link, and if it is false, display ... span> text. The address points to which the displayed link must perform data binding. It is this DataGrid's list of data bindings, and some rows do not have data bindings.
Functional requirements are analyzed here, so how to think now.
After I learned about the demand, I immediately reacted, using the bitchcolumn of DataGrid, hyperlinkcolumn, must not work. Moreover, it is also troublesome with TemplateColumn. He can only put some fixed controls such as HTML controls or server controls. Later I didn't have a way, ready to inherit the itemplate interface to develop a template column. Later I felt trouble. Because of the binding, you need something similar to the bound data source content similar to the BoundColumn feature. Later I found this method.
When viewing the .NET Framework SDK document, I found it in
<%
# DataBinder.eval (Container.DataItem, "FID")
%>
Such a data binding code, I want to join the code again?
I did a test, the test results found that the above code (including the previous and "<%>" symbols) output is actually an Object object, and then convert it into a string when DataGrid is displayed. Later I thought, can I add the code in front to call the method in the background code of this page.
We all know that in ASP.NET1.1, we have a name-called AAAA_ASPX when processing AAA.ASPX is handled in AAAA_ASPX. This class inherits its background code AAA.ASPX The class in .cs, this class inherits the system.web.ui.page class to achieve the required functions. That is to say, in ASP.NET 1.1, we can call any ways in the background CS class in the background CS class in the .aspx, which can call any methods in the background CS class. This will be done, since the data binding code from the above can get the primary key content (a string), then I can write a method in the background, this method has a string parameter, returning is also a string. This returned string is the content displayed in the column.
First, let's talk about the contents of the binding table, as follows
Column Type Use Fid System.String Table Primary Key Hassub System.Boolean Judging the address based on ContentURL System.String link
Then it is C # code in the background code.
protected
String
Getdgcolumn
String
ID)
{// Get the DataTable content from the data source DataTable DT =.; // Define the primary key column of DataTable Datacolumn [] key = {dt.columns ["fid"]}; dt.primaryKey = key; // According to incoming ID start looking for data line DATAROW ROW = DT.ROWS.FIND (ID); // After finding the judgment if ((Bool) Row ["Hassub"]) {// hasub is true, indicates return returno < Span style = / "/"> content span> ";} else {// hassub is listed as false, return to link Return" link content ";}}
Then define this in the daagrid in .aspx:
<
ASP: DATAGRID
Id
= "MyDataGrid"
Runat
= "Server"
Datakeyfield
= "FID"
AutogenerateColumns
= "False"
>
<
Columns
>
<
ASP: TemplateColumn
ItemStyle-Width
= ".."
.
>
<
ItemTemplate
>
<%
# Base.getdgcolumn ((object) DataBinder.eval (Container.DataItem, "FID")))
%>
ItemTemplate
>
ASP: TemplateColumn
>
Columns
>
ASP: DATAGRID
>
Using the data binding method used inside the DataGrid binding, the result is passed to the getDGColumn () method, and the content of the DataGrid column is actually the string returned by this method. Ok, a method of having to generate a DataGrid column is over, I don't know if you have a better way.
My shortcomings of this method are that there is no way to generate the server, which cannot be separated by the server segment, so it can only use static links, text, etc. There is also a JS script that can call the client. If you have a better way, if you click the event of the generated link button to be distinguished by the server, please advise. If you have a better way, please advise. I am here, thank you.