Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Finding a control inside a gridview row

How to find control inside a gridview row?

I have a gridview and inside this view I have a templatefield with server side asp.net button control.

I want to find grid view button control on row and want to stop displaying Button for non admin users.

Solution:

ASPX Code:

<asp:GridView ID="grdUserUpdateHistory" runat="server"  OnRowDataBound="OnRowDataBound" >
               

                <Columns>
 

                    <asp:BoundField HeaderText="UpdateDetail " DataField="UpdateTitle"></asp:BoundField>
                    
                    <asp:BoundField HeaderText="Version" DataField="Version"></asp:BoundField>
                    
                    <asp:BoundField HeaderText="UpdatedDate" DataField="ModifiedDate"></asp:BoundField>
                    
                    <asp:BoundField HeaderText="Modified By " DataField="ModifiedByUser.UserName"></asp:BoundField>
               <asp:TemplateField HeaderText="Delete Users" ShowHeader="True">
                                        <ItemTemplate>
     <asp:Button runat="server" ID="btnDelete " OnClick="BtnDeleteClick" Text="Delete"/>
                                        </ItemTemplate>
     </asp:TemplateField>
                </Columns>
               

            </asp:GridView>

C# code:

Find button control Using OnRowDataBound grid view event:

protected void OnRowDataBound(object Sender, GridViewRowEventArgs E)
        {
                if (E.Row.RowType == DataControlRowType.DataRow)
                {
                    var UserId = grdUserUpdateHistory.DataKeys[E.Row.RowIndex];
                       
                        if (UserId!=0)
                        {
                            var AdminUserEnebled =UserList.Where(U => U.userRole == "Admin");
                                                    
                            var btnDelete = E.Row.FindControl("btnDelete") as Button;
                           
                            if (AdminUserEnebled==null)
                            {
                                btnDelete.Visible = false;
                            }
                        }
                    } 
        }

              

Using Page event Page_load or you can use following logic in any custom method also:

protected void Page_Load(object sender, EventArgs e)
        {
 
            if (!Page.IsPostBack)
            {
 
                grdUserUpdateHistory.DataSource = yourDataSource;
                grdUserUpdateHistory.DataBind();
                for (int i = 0; i < grdUserUpdateHistory.Rows.Count; i++)
                {
                    Button deleteButton= (Button) grdUserUpdateHistory.Rows[i].FindControl("btnDelete");
                }
 
            }
        }

CS1026: ) expected (Asp.net button with ajax method call)

Facing following error with asp.net button, I used JavaScript Ajax method to cancel Approval.
Compiler Error Message: CS1026: ) expected

Source Code:

 

Line 99:  CommandName="Cancel"  Text="Cancel"  visible='<%# ShowCancelButton(Convert.ToString(Eval("Requester.FirstName"))) %>'/>--%>

Line 100:

Line 101: <asp:Button ID="CancelButton" runat="server" CssClass="wt-button" CausesValidation="False" OnClick=" CancelRequest(); return false;"

Line 102: CommandName="Cancel"  Text="Cancel" />

Line 103:

 

FIX:

With server side asp: Button  compiler will throw this error, use server side HTML button instead.

Source Code should be:

  <input type="button" ID="CancelButton" runat="server" Css="cs-button" CausesValidation="False" OnClick="
CancelRequest(); return false;" CommandName="Cancel"  Text="Cancel"  visible="True"  />

Finding a control inside a gridview footer row

How to find control inside a gridview footer row?

I want to find grid view HiddenField control on footer row and want to display total users count.

Solution:

ASPX Code:

<asp:GridView ID="grdUserUpdateHistory"   runat="server” OnRowDataBound="OnRowDataBound">
                
                <Columns>
                    
       <asp:BoundField HeaderText="UpdateDetail " DataField="UpdateTitle"></asp:BoundField>
       
       <asp:BoundField HeaderText="Version" DataField="Version"></asp:BoundField>
      
      <asp:BoundField HeaderText="UpdatedDate" DataField="ModifiedDate"></asp:BoundField>
    

<FooterTemplate>
  <asp:Label ID="totalUsers" runat="server" Text="" />
  <asp:HiddenField ID="HdnUsersCount" runat="server" />
</FooterTemplate>

         </Columns>
      </asp:GridView>

C# code:

Find button control Using OnRowDataBound grid view event:

/// <summary>
/// Handles the event of the Grid View Data Binding for row       
/// </summary>
protected void grdUserUpdateHistory_OnRowDataBound(object Sender,GridViewRowEventArgs E)
        {
            switch (E.Row.RowType)
            {
                  case DataControlRowType.DataRow:
                  // code to find grid row field
                  break;
                // code to find footer control
                case DataControlRowType.Footer:
                    var users= E.Row.FindControl("HdnUserCount") as HiddenField;
                    if (!string.IsNullOrEmpty(users) && users!= null)
                    {
                        userCount.Value = users;
                    }
                    break;
            }
        }