简体   繁体   English

如何在模板字段gridview中添加条件按钮?

[英]How to add conditional button in template field gridview?

I have one gridview that contains some fields, I want to show button in TemplateField when Process field has value. 我有一个包含一些字段的gridview ,我想在Process字段有值时在TemplateField中显示按钮。

Here is my code: 这是我的代码:

  <asp:GridView ID="grdList" PageSize="20" runat="server" DataSourceID="ODList" AllowPaging="True"
        AllowSorting="True" PagerSettings-Position="Top" AutoGenerateColumns="False"
        ShowFooter="True" ShowHeaderWhenEmpty="True" OnPageIndexChanged="grdList_PageIndexChanged">
        <Columns>
            <asp:BoundField DataField="MeasureCatalogId" SortExpression="MeasureCatalogId" />
            <asp:BoundField DataField="MeasureName"  SortExpression="MeasureName" />                
             <asp:TemplateField SortExpression="Process">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Process") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("LevelId") %>'></asp:TextBox>
                </EditItemTemplate>
            </asp:TemplateField>             
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <%if(????????) -- want condition for process!= ""
                      {
                    %>
                      <img class="semPopup" sempopupurl='<%=Constant.AppPath %>/forms/baseform/MeasureProcessFromCatalogForm.aspx?t=2&lid=<%# Eval("LevelId") %>&mid=<%# Eval("MeasureCatalogId") %>'
                                    sempopupwidth="<%=width %>" sempopupheight="<%=height %>"
                                    src="../../App_Themes/images/select2.gif" />
                    <%} %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <PagerSettings Position="Top"></PagerSettings>
        <PagerStyle CssClass="grid_pager" />
    </asp:GridView>

As an alternative to the other recommendations; 作为其他建议的替代方案; you could try using a code expression on the field: 你可以尝试在字段上使用代码表达式:

   <asp:TemplateField ShowHeader="False">
            <ItemTemplate>                
                  <img class="semPopup" sempopupurl='<%=Constant.AppPath %>/forms/baseform/MeasureProcessFromCatalogForm.aspx?t=2&lid=<%# Eval("LevelId") %>&mid=<%# Eval("MeasureCatalogId") %>'
                                sempopupwidth="<%=width %>" sempopupheight="<%=height %>"
                                src="../../App_Themes/images/select2.gif" Visible='<%# ShowButton(Eval("Process")) %>'/>
            </ItemTemplate>
        </asp:TemplateField>

I have not tried this out, but it follows a similar pattern to what i have previously used. 我没有试过这个,但它遵循了我以前使用过的类似模式。 The code expression calls the codebehind function 'ShowButton' which should return a bool, here you can evaluate the value of process passed in, and if it is to your liking, pass back a true value; 代码表达式调用代码隐藏函数'ShowButton',它应该返回一个bool,在这里你可以评估传入的进程的值,如果它是你喜欢的,则传回一个真值; otherwise return false and the button will not show. 否则返回false,按钮不会显示。

C# C#

protected bool ShowButton(object DataItem)
    {
        //Here you can place as many conditions as you like 
        //Provided you always return either true or false
        if (DataItem != null)
            return true;
        else
            return false;
    }

A suggested edit was posed in a similar fashion, however instead of a bool visibility, the visibility setting is altered by string value. 建议的编辑以类似的方式提出,但是不是bool可见性,而是通过字符串值更改可见性设置。 I am sure they will add their take too. 我相信他们也会加分。 After all, variety is the spice of life 毕竟,多样性是生活的调味品

Use codebehind, make the button visible when appropriate: 使用代码隐藏,在适当的时候使按钮可见:

protected void gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DataRow row = ((DataRowView)e.Row.DataItem).Row;
        string process = row.Field<string>("Process"); // change type from string to whatever it is
        Button btn = (Button) e.Row.FindControl("ButtonID");
        btn.Visible = process == "YourProcessValue";
    }
}

you can make use of RowDataBoundand than make decision of hiding your control 你可以使用RowDataBoundand而不是决定隐藏你的控件

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SomeObject mapItem = (SomeObject)e.Row.DataItem;
            if(!mapItem.Process)
                   (e.Row.Cells[3].FindControl("Buttonid") as Button).visible= false;
        }         
    }

or try 或尝试

      <ItemTemplate>
            <asp:Button runat="server" Text="Reject" 
            Visible='<%# ((bool)Eval("Process")) %>' />
        </ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM