【问题标题】:datagrid text field that trigger GridView1_RowCommand event触发 GridView1_RowCommand 事件的数据网格文本字段
【发布时间】:2016-08-25 01:43:51
【问题描述】:

我有一个数据网格,我希望将字段显示状态作为文本并触发一个命令事件,这样我就可以像 asp:ButtonField 一样在服务器端处理它。

我的失败代码是:

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

我得到错误:

文字内容 ('" >') 不允许在 '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>

我也尝试过使用&lt;asp:TemplateField&gt;,但没有成功

我怎样才能获得一个字段来显示数据并成为一个可点击/触发的事件?

【问题讨论】:

    标签: asp.net datagrid


    【解决方案1】:

    ButtonField 不支持绑定表达式。您可以将其替换为带有 LinkBut​​ton 的 TemplateField 或 ItemTemplate 中的按钮。在Text属性的数据绑定表达式中,应将外层双引号替换为单引号,避免与内层双引号冲突:

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

    【讨论】:

    • 我怎样才能将 dataKey(也就是行的 id)传递给 .cs 事件,所以我可以这样做:protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); if (e.CommandName.Equals("detail"))
    • 您可以将CommandArgument绑定到ID字段。我更新了我的答案以表明这一点。您可以调整代码以适合您的实际 ID 字段名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 2023-03-20
    • 2012-05-16
    相关资源
    最近更新 更多