【问题标题】:How can I use the GridView datasource in code-behind to not trim text?如何在代码隐藏中使用 GridView 数据源不修剪文本?
【发布时间】:2012-12-12 18:37:28
【问题描述】:

您好,感谢您的关注。

我有一个 GridView,它的数据源设置为一个返回 ArrayList 的外部类。现在,在 aspx 页面中,我有一个 TemplateField,并且 Text 属性设置为

Text = '<%#Eval("Name") %>'

除了名称总是被截断,即使数据库中的名称具有更大的值。我猜gridview/databinding出于某种原因截断了名称?无论如何,我想在悬停时显示全名,所以在 _RowDataBound 事件上,我有 e.Row.Cells[2].ToolTip = something,我不确定那个“东西”应该是什么。

在这种情况下我也可以使用 Eval 吗?如果是这样,语法会是什么样的?如果没有,我有什么选择?

【问题讨论】:

    标签: c# gridview eval code-behind rowdatabound


    【解决方案1】:

    小玩CSS 和设置tooltip 会解决这个问题

    代码隐藏:

       protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {   for(int i =0;i <GridView1.Rows.Count;i++)
            {
               Label lblName = (Label)GridView1.Rows[i].Cells[1].FindControl("lblname");
               lblName.ToolTip = "This is my toolTip";//  You can set tooltip as Fullname  
            }
        }
    

    Default.aspx:

       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="gridMyClass"
            Width="125px" onrowdatabound="GridView1_RowDataBound">
            <Columns>
                <asp:TemplateField HeaderText="ID">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%#Eval("Id") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                      <asp:Label ID="lblname" CssClass="myTxtClass"  runat="server" Text='<%#Eval("name")%>'></asp:Label>
    
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    

    CSS:

    .myTxtClass{
        display:inline;
        position: relative;
        text-decoration:none;
        cursor:pointer;
    }
    
    .myTxtClass:hover:before{
        border: solid;
        border-color: red transparent;
        border-width: 6px 6px 0 6px;
        bottom: 20px;
        content: "";
        left: 50%;
        position: absolute;
        z-index: 99;
    }
    
    
    .myTxtClass:hover:after{
        background-color: red;
        opacity:0.7;
        border-radius: 5px;
        bottom: 26px;
        color: #fff;
        content: attr(title);
        left: 20%;
        padding: 5px 15px;
        position: absolute;
        z-index: 98;
        width: 220px;
    } 
    

    屏幕截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 2013-09-07
      • 2013-10-15
      • 2011-08-26
      • 1970-01-01
      • 2022-12-01
      相关资源
      最近更新 更多