asp.net|datagrid| control has recently written several asp.net projects. Among them, the ASP.net control datagrid is used most, a few days ago need a function, I studied for a long time, found a method, should be the simplest implementation. The present statement is as follows.
The HTML definition for the DataGrid 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 for functionality are in ItemTemplate, which may be a binding data source:
<a href= "..." >...</a>
Link code, there may also be a
<span>...</span>
Displays the text code, which is based on the contents of a particular column in each row of the DataTable that is bound to the data source in the DataGrid.
Plainly, the function is the DataGrid. When binding data generation rows, determine the contents of a field in the DataRow of each row of the data source DataTable, this field type is bool type, and if true, this column displays <a href= "..." > ...</a> the link, and if it is false, the <span>...</span> text is displayed. The address pointed to in the link in the display must perform data binding. This is the column of the DataGrid. There are rows to be data bound, and some rows are not data bound.
Functional requirements analysis Here, so now it's time to think about how to achieve.
I then understand the demand, immediately reaction out, using the DataGrid from the BoundColumn, HyperLinkColumn certainly not. Also, there is trouble using TemplateColumn. He can only put some fixed controls, such as HTML controls or server controls. Later, I have no way, ready to inherit the ITemplate interface to develop a template column. And then I felt trouble. Because the binding requires a binding data source content similar to the BoundColumn feature. Then I found out about this method.
While viewing the. Net Framework SDK documentation, I found that the <ItemTemplate> in <Asp:TemplateColumn> can be put in a similar
<%# DataBinder.Eval (Container.DataItem, "FID")%> such data-binding code, I wondered if I could add code.
I did a test, and the test results found that the above code (including the front and rear <%%> symbols) actually outputs an object and then converts it to a string when the DataGrid is displayed. Later I thought, whether can add code before, call this page of the background code in the method.
As we all know, in asp.net1.1, a asp.net page called aaa.aspx is a class called asp.aaa_aspx when it is processed inside ASP.net, This class inherits the class from its background code Aaa.aspx.cs, which inherits the System.Web.UI.Page class to achieve the desired functionality. That is to say, in ASP.net 1.1, the code contained in <%%> in. aspx can invoke any method of public and protected life in the background CS class.
That's fine, since the data binding code above can get the primary key content (is a string), then I can write a method in the background, this method has a string parameter, the return is also a string. The returned string is the content that is displayed in the column.
First, let's say the contents of the binding table, as follows
Column name type use
FID System.String Table Primary key
The basis of hassub System.Boolean judgment
contentURL System.String The address of the link
Then the C # code in the background code
Protected string Getdgcolumn (string id)
{
Get the DataTable content from the data source slightly
DataTable dt =.;
Defines a primary key column for a DataTable
Datacolumn[] key = {dt. columns["FID"]};
Dt. PrimaryKey = key;
Start looking for data rows based on incoming IDs
DataRow row = dt. Rows.find (ID);
When you find it, start judging.
if ((bool) row["Hassub"])
{
Hassub is true to return <span>
Return "<span style=\" \ "> Content </span>";
}
Else
{
Hassub listed as false, return link
Return "<a href=\" xxxx.aspx?id= "+id+" \ "target=\" _blank\ "> Link content </a>";
}
}
The data binding method used internally by the DataGrid binding is used to obtain the result, and then the result is passed to the Getdgcolumn () method, and the contents of the DataGrid column are actually the string returned by the method used.
Well, a way to generate the contents of the DataGrid column is finished, I do not know if we have a better way.
The disadvantage of my method is that the generated content is not able to execute the server submission, it can not be separated from the server segment, so can only be used static links, text and so on. There is a JS script that can invoke the client. If someone has a better method, such as the generated Link button Click event can be differentiated by the server side, please advise. If you have a better way, please advise. I'm here to thank you.
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.