1

我有包含 ImageButton 的 w 网格。我想从 aspx 页面设置工具提示,例如:

The user's id is <user_id> and his name is <user_name>

whereuser_iduser_name是网格数据集中的字段。如何实现这一点?这是我的代码:

 <asp:TemplateField HeaderText="">
    <HeaderStyle HorizontalAlign="Left" />
    <ItemStyle HorizontalAlign="Left" />
    <ItemTemplate>
        <asp:ImageButton ID="btnViewUser" runat="server" ToolTip=' <%# Eval("user_id") %>'
                                                        AlternateText="View or edit the record" ImageUrl="~/images/Icon/view_text.png"
                                                        CommandName="ViewOrder" CommandArgument='<%# Container.DisplayIndex %>' />
     </ItemTemplate>
</asp:TemplateField>

谢谢。

4

1 回答 1

0

最简单和最可维护的是使用代码隐藏,RowDataBound是最好的地方:

protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton btnViewUser = (ImageButton) e.Row.FindControl("btnViewUser");
        DataRow row = ((DataRowView) e.Row.DataItem).Row;
        btnViewUser.Tooltip = string.Format("The user's id is {0} and his name is {1}"
            , row.Field<int>("user_id")
            , row.Field<string>("user_name"));
    }
}

这是 aspx 方法(不确定这是否有效,我永远不会使用这样的代码):

<asp:ImageButton id="btnViewUser" runat="server"
           Tooltip='<%# "The users id is "+ DataBinder.Eval(Container.DataItem, "user_id") +" and his name is "+ DataBinder.Eval(Container.DataItem, "user_name") +"" %>'
           AlternateText="View or edit the record" 
           ImageUrl="~/images/Icon/view_text.png"
           CommandName="ViewOrder" 
           CommandArgument='<%# Container.DisplayIndex %>' />
于 2013-10-23T07:04:19.937 回答