asp.net get back or front of the binding text TemplateField

GridView most used one is BoundField, there is a TemplateField
both have their own characteristics, BoundField simple words, set the DataField, HeaderText okay and so on up.
But sometimes with TemplateField then, even more interactivity.
The project, the other side wants us to do two header table, it uses TemplateField, code format is as follows:
``
```
<asp:TemplateField HeaderStyle-Width="250px">
                    <HeaderTemplate>
                        <table width="100%" align="center">
                            <tr>
                                <td colspan="3" width="100%" align="center">
                                     详细信息
                                </td>
                            </tr>
                            <tr>
                                <td  width="70%" align="center">
                                   描述
                                </td>
                                <td  width="15%" align="center">
                                    图片(张)
                                </td>
                                <td  width="15%" align="center">
                                    视频(段)
                                </td>
                            </tr>
                        </table>
                    </HeaderTemplate>
                    <ItemTemplate>
                        <table width="100%">
                            <tr>
                                <td width="70%" align="center">
                                  <asp:Label id="Label1" runat="server" Text=<%# Eval("data1"/asp:Label>
                                </td>
                                <td width="15%" align="center">
                                <asp:HyperLink id="HyperLink1 Text=<%# Eval("data2"%> ForeColor="#00d3d4" NavigateUrl='' runat="server" title='查看' CssClass="hylink"></asp:HyperLink>     
                               </td>
                                <td width="15%" align="center">
                                <asp:HyperLink id="scanVideo" Text=<%# Eval("data3"%> ForeColor="#00d3d4" NavigateUrl='' runat="server" title='查看' CssClass="hylink"></asp:HyperLink>  
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                    <The HeaderStyle the Width = "250px"> </ the HeaderStyle>
                </ ASP: the TemplateField>
`` `
after the isolated data from the database in the background by the control, the field corresponding to the binding, is displayed on the browser:
wherein pictures and HyperLink video is a link you can click to view the pictures and videos ~ ~
 
  •  - get the front end of the binding text TemplateField
But I hope, only the number of pictures and videos are time greater than zero is blue-green, and you can click to 0 when the same as a normal binding. Js used here distal to obtain
```
$(document).ready(function() {
     $('select').addClass("form-control");
     var gdview = document.getElementById("GridView1");
     var GridView1RowsLength = getTableRowsLength("GridView1");
     for (var j = 1; j < GridView1RowsLength; j++) {
 var HTML = gdview.rows[j].cells[6].children[0];//获取到TemplateField这一列,我这里是cells[6]
 var photoNum = HTML.rows[0].cells[1].textContent;
        var vedioNum = HTML.rows[0].cells[2].textContent;
        if (photoNum == 0) {
             HTML.rows[0].cells[1].children[0].href = "#";
             HTML.rows[0].cells[1].children[0].title = "";
             HTML.rows[0].cells[1].children[0].style.color = "rgb(51, 51, 51)";
         }
         if (vedioNum == 0) {
             HTML.rows[0].cells[2].children[0].href = "#";
             HTML.rows[0].cells[2].children[0].title = "";
             HTML.rows[0].cells[2].children[0].style.color = "rgb(51, 51, 51)";
         }
}
 });
 
function getTableRowsLength(id) {
     var mytable = document.getElementById(id);
     return mytable.rows.length;
 }
```
效果:

0 mouse is placed up there will be no title displayed, click there will not be a reaction
 
 
  •  - get the text back-end binding TemplateField
 The foregoing description text, the other party to the beginning of the template is less than 10 characters, so no special treatment, and later mentioned other descriptive text usually about 50 words, of course, impossible to direct so many characters are displayed, accounting also ugly position, so think after more than a fixed number of words, followed by ... representation, it can be placed on the mouse when the current cell suspension all description text. Here with a background control:
 first thing to say is, in fact, the beginning, my description is placed directly on the `<td> </ td> ` in, but so although you can get back to the text, but can not use the ToolTip, nor Text assignment its properties, and later with `<td> <span title = " "> <% # Eval (" data1 ")%> </ span> </ td>`, which can (js code that is above the front end in) control, display effect I want, but the front is mandatory generated because the binding on the gridview data or text a long period, so just refresh the page when the text will see a description of a long segment of flash for a moment, you will want to become "part of the description text" + ... so, so I hope when data binding has to deal with these
 on the internet looking for a long time, and the method is not found, only on her own
 code is as follows:
 
`` `
/// <the Summary>
    /// when the event time of each row of data Gridview bound to trigger
    /// </ the Summary>
    /// <param name =" SENDER "> < / param>
    /// <param name = "E"> </ param>

    {
 if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[6].ToString().Trim().Length > 12)  //描述的文字长度大于12的时候
            {
                string dis = ((System.Data.DataRowView)(e.Row.DataItem)).Row.ItemArray[6].ToString().Trim();
                Label aaa = e.Row.FindControl("Label1") as Label; 
                aaa.ToolTip = dis;
                aaa.Text = dis.Substring(0, 11) + "...";
            }
}
}
```
((System.Data.DataRowView) (e.Row.DataItem)). Row.ItemArray [6] This is how to get it, that I initially use the e.Row.Cells [] but get less later use ((DataBoundLiteralControl) row.Cells [index] .Controls [0]). Text.Trim () is also a little problem, the background debugger, get e.row, found the entire row of data, in layer by layer find information described in the cell, and the expression obtained by direct copying.
Then to complete, in fact, before the two digits can also use the back-end approaches, to get the same text, to make a judgment, HyperLink PhotoNum = e.Row.FindControl ( "HyperLink1") as HyperLink; then set the properties on the list. (=.
After word limit, the mouse is placed - display the full text suspension
 

Guess you like

Origin www.cnblogs.com/zq77ssi/p/11352758.html