Asp.net 在ImageButton工具提示中使用硬编码字符串与数据集中的值连接

Asp.net 在ImageButton工具提示中使用硬编码字符串与数据集中的值连接,asp.net,gridview,tooltip,imagebutton,Asp.net,Gridview,Tooltip,Imagebutton,我有一个包含ImageButton的w网格。我想从aspx页面设置工具提示,类似于: The user's id is <user_id> and his name is <user_name> 谢谢。最简单和最易维护的方法是使用codebehind,RowDataBound是最好的地方: protected void Gridview1_RowDataBound(Object sender, GridViewRowEventArgs e) { if (e.Ro

我有一个包含ImageButton的w网格。我想从aspx页面设置工具提示,类似于:

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

谢谢。

最简单和最易维护的方法是使用codebehind,
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"));
    }
}
受保护的无效Gridview1\u行数据绑定(对象发送方,GridViewRowEventArgs e)
{
如果(e.Row.RowType==DataControlRowType.DataRow)
{
ImageButton btnViewUser=(ImageButton)e.Row.FindControl(“btnViewUser”);
DataRow row=((DataRowView)e.row.DataItem).row;
btnViewUser.Tooltip=string.Format(“用户的id是{0},他的名字是{1}”
,行。字段(“用户id”)
,row.Field(“用户名”);
}
}
以下是aspx方法(不确定是否有效,我永远不会使用这样的代码):



但我想在aspx文件中实现这一点,如上所述,类似于Tooltip='',但与我的硬编码字符串和其他值连接在一起,我已经更新了问题并添加了源代码code@gabi当前位置编辑了我的答案。这很有效,我只需逃出“角色”。谢谢
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"));
    }
}
<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 %>' />