0

I have an <asp:ButtonField> inside a gridview. How do I target the button in the gridview to make it Visible on runtime based on a condition ? I am not able to target it since it doesnt have the ID property. I am stuck here. Here is the code below

<asp:GridView ID="OrdersDataList1" runat="server"  DataKeyNames="OrderID" Width="100%" SkinID="Gridview" OnPageIndexChanging="orders_PageIndexChanging" 
    EmptyDataText="You have no orders." AllowSorting="True" OnSorting="OnSort" AllowPaging="true" PageSize="15" AutoGenerateColumns="False" OnRowCommand="updateStatus">
    <Columns>
        <asp:BoundField DataField="CustomerUser_ID" HeaderText="UserID" Visible="true" />
        <asp:BoundField DataField="OrderID" HeaderText="OrderNo" InsertVisible="False" ReadOnly="True" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left" SortExpression="OrderID" />
        <asp:BoundField DataField="OrderDate" HeaderText="OrderDate" SortExpression="OrderDate" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"/>
        <asp:BoundField DataField="Base" HeaderText="Base" DataFormatString="{0:C}"   SortExpression="Base" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
        <asp:BoundField DataField="Freight" HeaderText="Freight" DataFormatString="{0:C}"   SortExpression="Freight" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
        <asp:BoundField DataField="Total" HeaderText="Total" DataFormatString="{0:C}"   SortExpression="Total" HeaderStyle-HorizontalAlign="Right" ItemStyle-HorizontalAlign="Right"/>
        <asp:BoundField DataField="Products" HeaderText="Products" SortExpression="Products" DataFormatString="{0} product" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
        <asp:BoundField DataField="Units" HeaderText="Units" SortExpression="Units"  DataFormatString="{0} units" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
        <asp:BoundField DataField="OrderStatusName" HeaderText="Current Status" SortExpression="OrderStatusName" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
        <asp:BoundField DataField="OrderStatusID" HeaderText="Orderstatusid" Visible="true" />
        <asp:BoundField DataField="OrderTracking_ID" HeaderText="TrackingNo" SortExpression="OrderTracking_ID" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"/>
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <%# GetViewOrderLink(Eval("OrderID").ToString(), Eval("OrderState").ToString())%>
            </ItemTemplate>
            <HeaderStyle HorizontalAlign="Left" />
            <ItemStyle HorizontalAlign="Left" />
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Change Status" SortExpression="OrderStatusName">
            <ItemTemplate>
                <asp:DropDownList ID="OrderStatusDD" runat="server"
                    DataSourceID="OrdersStatuses" DataTextField="OrderStatusName" DataValueField="OrderStatusID" Visible="false">                                      
                </asp:DropDownList>
                <asp:SqlDataSource ID="OrdersStatuses" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:SqlConn %>" 
                    SelectCommand="SELECT [OrderStatusID], [OrderStatusName] FROM [Orders_Statuses] where OrderStatusID = 2 or OrderStatusID = 8 ORDER BY [OrderStatusName]">
                </asp:SqlDataSource>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:ButtonField ButtonType="Button" Text="Update status" Visible="false" HeaderText="Change Status" />
    </Columns>
</asp:GridView>

PS I have the onrowCommand set on the gridview to listen to button click in gridview

Muhammed Shevil KP
  • 1,404
  • 1
  • 16
  • 21
Varun Pozhath
  • 608
  • 6
  • 19
  • i think you need to convert the buttonfield into a templatefield.. that way you can assign it with an ID – eirishainjel Feb 01 '17 at 05:08
  • Also, make it hidden not invisible. Setting `YourButton.Visible=false` prevents it from being rendered and will require a postback to make it visible. Much cleaner add a piece of JS or JQ on your DDL select and change the button from `hidden=true` to `hidden=false` or `display:none` to `display:inline` or however you want to do it. – fnostro Feb 01 '17 at 17:56
  • Keep in mind that changing visibility may introduce some unappealing screen motion due to the button being revealed. You should account for that or maybe consider making all the buttons visible and altering it's enable/disabled state. – fnostro Feb 01 '17 at 17:58

3 Answers3

0

You can use the RowCreated event in the GridView as follows:

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        Button btn = (Button) e.Row.Cells[12].Controls[0];


        if(1==1)
        {

            btn.Visible = true;

        }
    }
Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
InnovateIt
  • 88
  • 5
0

You need to create RowCreated event for the gridview example

Before the GridView control can be rendered, a GridViewRow object must be created for each row in the control. The RowCreated event is raised when each row in the GridView control is created. This enables you to provide an event-handling method that performs a custom routine, such as adding custom content to a row, whenever this event occurs.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
}

OR

<asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Age" HeaderText="Age" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" Text="Reject" 
                Visible='<%# IsOverAgeLimit((Decimal)Eval("Age")) %>' 
                CommandName="Select"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

protected Boolean IsOverAgeLimit(Decimal Age) {
    return Age > 35M;
}

Reference example

Community
  • 1
  • 1
Learning
  • 19,469
  • 39
  • 180
  • 373
-1
((BoundField)grv_selec.Columns[1]).DataFormatString = "{0:N2}";
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • 2
    Hello Carlos, please add context and add a description. Code-only on-line answers are highly discouraged on this site ! – JB. Feb 16 '21 at 20:56