2016-04-29 61 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