2016-04-29 56 views
1

我有一個datagrid,我想有一個字段的顯示狀態作爲文本,並觸發命令事件,所以我可以像asp:ButtonField那樣處理它的服務器端。datagrid文本字段,觸發GridView1_RowCommand事件

我失敗的代碼是:

<asp:ButtonField CommandName="CallHist" 
ButtonType="Button" Text ="<%#GetJobs(Eval("currStatus"))%>" > 
</asp:ButtonField> 

我得到錯誤:

Literal content ('" >') is not allowed within a 'System.Web.UI.WebControls.ButtonField'.

我試圖改變現有workign ButtonFields:

<asp:ButtonField CommandName="editRecord" ControlStyle-Height="16px" ControlStyle-Width ="16px" ButtonType="image" ImageUrl="images/edit-icon.png"> 
              </asp:ButtonField> 

              <asp:ButtonField CommandName="deleteRecord" ControlStyle-Height="16px" ControlStyle-Width ="16px" ButtonType="Image" ImageUrl="images/close-icon.png"> 
              </asp:ButtonField> 

我也試過了,沒有運氣好,用<asp:TemplateField>

我如何獲得一個字段來顯示數據,併成爲可點擊/觸發的事件?

回答

1

ButtonField不支持綁定表達式。您可以使用LinkBut​​ton或ItemTemplate中的按鈕將其替換爲TemplateField。在Text屬性的數據綁定表達式,則應更換用單引號外雙引號,以避免與內雙引號衝突:

<asp:TemplateField> 
    <ItemTemplate> 
     <asp:LinkButton runat="server" CommandName="CallHist" Text='<%# GetJobs(Eval("currStatus")) %>' CommandArgument='<%# Eval("ID") %>' /> 
    </ItemTemplate> 
</asp:TemplateField> 
+0

我怎麼能得到這個通過dataKey(又名ID (row))到.cs事件,所以我可以做這樣的事情:保護無效GridView1_RowCommand(對象發件人,GridViewCommandEventArgs e) int index = Convert.ToInt32(e.CommandArgument); if(e.CommandName.Equals(「detail」)) – kacalapy

+0

您可以將'CommandArgument'綁定到ID字段。我更新了我的答案以證明這一點。您可以調整代碼以符合您的實際ID字段名稱。 – ConnorsFan