2013-04-16 47 views
2

我必須爲我的gridview編輯圖像按鈕添加工具提示。我編碼爲Commandfield。如何爲Commandfield ButtonType添加工具提示作爲圖像

<asp:CommandField ButtonType="Image" 
    HeaderStyle-HorizontalAlign="center" 
    EditText="Edit" UpdateText="Edit" 
    ShowEditButton="True" 
    ItemStyle-VerticalAlign="Top" 
    EditImageUrl="~/Images/edit.png"  
    UpdateImageUrl ="~/Images/Save.png" CancelImageUrl ="~/Images/cancel.png" 
    ItemStyle-Width="15px" ControlStyle-Width="15px"> 
</asp:CommandField> 

是否可以添加一個工具提示在gridview的命令行按鈕類型爲圖像?

回答

3

下面的代碼工作很好:

protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e) 
     { 
      try 
      { 
       if (e.Row.RowType == DataControlRowType.DataRow) 
       { 
        e.Row.Cells[4].ToolTip = "Edit Resource Details"; 
        if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState.ToString() == "Alternate, Edit") 
        { 
         int i = 0; 
         foreach (TableCell cell in e.Row.Cells) 
         { 
          if (e.Row.Cells.GetCellIndex(cell) == 4) 
          { 
           ((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[0])).ToolTip = "Update Resource Details"; 
           ((System.Web.UI.LiteralControl)(e.Row.Cells[4].Controls[1])).Text = "&nbsp;"; 
           ((System.Web.UI.WebControls.ImageButton)(e.Row.Cells[4].Controls[2])).ToolTip = "Close Resource Details"; 
          } 
          i++; 
         } 
        } 
       } 
      } 
      catch (Exception _e) 
      { 
      } 
     } 
0

,或者你可以

  1. 定義你的GridView的CommandField的一個EditText上屬性(像你一樣),這將呈現爲alt屬性附加傷害的<input type='image' alt='Edit' />標籤: <asp:CommandField ButtonType="Image" EditText="Edit" etc />

  2. 然後添加腳本標記來設置title屬性用下面的代碼:

VB:

Protected Sub gvResourceEditor_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvResourceEditor.RowDataBound 
    Dim strScript as String = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});" 
    Page.ClientScript.RegisterStartupScript(Me.GetType, "SetEditButtonTitle", strScript, True) 
End Sub 

CS:

protected void gvResourceEditor_RowDataBound(object sender, GridViewRowEventArgs e) { 
    string strScript = "$('#" + gvResourceEditor.ClientID + " input[type=image]').each(function(key, el) {el.title=el.alt;});" 
    Page.ClientScript.RegisterStartupScript(this.GetType(), "SetEditButtonTitle", strScript, true); 
} 

當然,你可能需要泰勒javascript來網格。該腳本將使用type=image來設置所有input標記的標題屬性。

0

我沒有嘗試SekaranShow建議的解決方案,採取了一些不同的方法,因爲我不願意依靠JavaScript;認爲這可能值得分享。

表演在他的解決方案,編輯按鈕的EditText屬性將被呈現爲圖像的alt屬性提及;這意味着它也應該存儲爲.NET對應項的AlternateText。 就我而言;我在我的CommandField中有Edit,Update,DeleteCancel按鈕,我想對所有按鈕做同樣的操作。

所以(本地化)文本是在AlternateText屬性的按鈕;我正在循環顯示每個按鈕,並將該值放在ToolTip屬性中。 這是我的代碼。

protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     foreach (Control ctrl in e.Row.Cells[2].Controls) 
     { 
      if (ctrl.GetType().BaseType == typeof(ImageButton)) 
       ((ImageButton)ctrl).ToolTip = ((ImageButton)ctrl).AlternateText; 
     } 
    } 
} 

我在代碼中硬編碼了Cell ID「2」,因爲它是衆所周知的。 每個圖片按鈕控件是System.Web.UI.WebControls.DataControlImageButton類型,不可訪問。所以我用它的BaseType應該是ImageButton