A simple method to control the content of the template column (templatecolumn) of the DataGrid in ASP. NET.

Source: Internet
Author: User
Recently I wrote several Asp.net projects. Among them, the control DataGrid of ASP. NET is the most used, and a function is required a few days ago. I have studied it for a long time and found that a method should be the simplest implementation. The statement is as follows.

The html definition of the DataGrid is very 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 functional requirement is that in itemtemplate, a data source may be bound:

<A href = "...">... </a>

LinkCodeOr it may be

<Span>... </span>

The text code is displayed, and the judgment is based on the content of a specific column in each row of the able bound to the data source in the DataGrid.

To put it bluntly, the function is to determine the content of a field in datarow in each row of the data source datatable when the DataGrid is bound to a data generation row. The field type is bool. If it is true, <a href = "... ">... </a> link. If it is false, <span>... </span> text. Data Binding is required for the address indicated in the displayed link. That is, some rows in the column of this DataGrid need data binding, and some rows do not need data binding.

The functional requirements are analyzed here, so now we should think about how to implement it.

After I understood the requirements, I immediately responded. Using the boundcolumn and hyperlinkcolumn that comes with the DataGrid will certainly not work. It is also troublesome to use templatecolumn. It can only hold some fixed controls, such as HTML controls or server controls. Later, I had no choice but to inherit the itemplate interface and develop a template column by myself. Later, I felt troublesome. This is because you need something similar to the boundcolumn function to bind the data source. Later I found this method.

When viewing the. NET Framework SDK documentation, I found that <itemtemplate> in <asp: templatecolumn>
<% # Databinder.Eval(Container. dataitem,"FID") %>

With this data binding code, I wonder if I can add the code again.

I did a test and found that the above Code (including the <%> symbol at the beginning and the end) actually outputs an object, then convert it to a string when the DataGrid is displayed. Later I thought about whether I could add the code before and call the method in the background code of this page.

We all know that in ASP. in net1.1, the name is AAA. when the Asp.net page of aspx is processed internally in Asp.net, it is called ASP. aaa_aspx class, which inherits its background code AAA. aspx. class in CS, which inherits system. web. UI. page class to implement the required functions. That is to say, in Asp.net 1.1, any method in the. aspx containing code <%> can call the public and protected life In the backend CS class.

This is easy to handle. Since the data binding code above can obtain the primary key content (a string), I can write a method in the background. This method has a string parameter, returns a string. The returned string is the content displayed in the column.

The content of the bound table is as follows:

Column name Type Purpose
FID System. String Table Primary Key
Hassub System. Boolean Judgment basis
Contenturl System. String Link address

Then the C # code in the background code
Protected   String Getdgcolumn ( String ID)

{

// Retrieving datatable content from the data source

Datatable dt = .;

// Define the primary key column of the datatable

Datacolumn [] Key =   {DT. Columns ["FID"]} ;

DT. primarykey = Key;


// Start to find data rows based on the input ID

Datarow row = DT. Rows. Find (ID );


// Locate and start judgment

If (( Bool ) Row [ " Hassub " ])

{

//If hassub is true, <span> is returned.

Return "<Span style = \"\"> Content </span>";

}

Else

{

// If the hassub column is set to false, a link is returned.

Return   " <A href = \ " XXXX. aspx ? ID = " + ID + "\ " Target = \ " _ Blank \ " > Link content </a> " ;

}

}

The definition is as follows in 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 >

The data binding method used internally during the DataGrid binding is used to obtain the result, and then the result is passed to the getdgcolumn () method. The content of the DataGrid column is actually a string returned by this method.

Now, we have finished generating the content of the DataGrid column as you like. I wonder if you have any better method.

The disadvantage of this method is that the generated content cannot be submitted by the server. It cannot be separated by server segments. Therefore, it can only use static links and texts. In addition, you can call the client's Js script. If you have a better method, for example, the Click Event of the generated link button can be distinguished by the server, please advise. If you have a better method, please advise. I am here to thank you all.

Related Article

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.