[SOLVED] Unique ID's for buttons in gridview?

Issue

A 508 Scan flagged the fact that we have identical id’s for buttons in gridviews, and I’m wondering how to get around this. For some of these buttons we can remove the id’s entirely and they don’t seem to affect functionality, but others actually do have back-end code that enables/disables them based on other factors. Here’s the front-end code:

<asp:TemplateField HeaderText="Accept" ItemStyle-HorizontalAlign="Center">
                                    <ItemTemplate>
                                        <asp:Button
                                            ID="btnAccept"
                                            runat="server"
                                            Text="Accept Data"
                                            CssClass="inherited"
                                            CommandName="AcceptData"
                                            CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
                                    </ItemTemplate>
                                    <ItemStyle HorizontalAlign="Center"></ItemStyle>
                                </asp:TemplateField>

And here is some of the backend code that pertains to this button:

protected void gvDashboard_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int rowIndex = e.Row.RowIndex;

                Button btnAccept = e.Row.FindControl(@"btnAccept") as Button;
                Button btnRemove = e.Row.FindControl(@"btnRemove") as Button;

                bool errors = (bool)gvDashboard.DataKeys[e.Row.RowIndex][@"Errors"];
                string fileType = (string)gvDashboard.DataKeys[e.Row.RowIndex][@"FileType"];
                string processingStatus = (string)gvDashboard.DataKeys[e.Row.RowIndex][@"ProcessingStatus"];
                bool accepted = (bool)gvDashboard.DataKeys[e.Row.RowIndex][@"Accepted"];

                int records = 0;
                if (gvDashboard.DataKeys[e.Row.RowIndex][@"Records"] is System.DBNull)
                {
                    int recordsColumnIndex = Utilities.GetColumnIndexByHeaderText(gvDashboard, @"Records");
                    e.Row.Cells[recordsColumnIndex].Text = @"0";
                }
                else
                {
                    records = (int)gvDashboard.DataKeys[e.Row.RowIndex][@"Records"];
                }

                
                if (fileType == @"CN" || fileType == @"CP")
                {
                    if ((DBNull.Value.Equals(gvDashboard.DataKeys[e.Row.RowIndex][@"ZipId"]))
                        || ((int)gvDashboard.DataKeys[e.Row.RowIndex][@"ZipId"] == 0))
                    {
                        // CN or CP did not come from a zip file
                        if ((accepted))
                        {                                
                            btnAccept.Enabled = false;                                
                            btnRemove.Enabled = false;
                        }
                    }
                    else
                    {
                        btnAccept.Enabled = false;
                        btnRemove.Enabled = false;
                    }
                }
                
                if (accepted || processingStatus == @"I" || processingStatus == @"N")
                {
                    btnAccept.Enabled = false;
                }
            }
        }
        catch (Exception ex)
        {
            DAL.ErrorLog(@"FilteredDashboard.gvDashboard_RowDataBound: Row: " + e.Row.RowIndex.ToString() + "  " + ex.Message);
        }
    }

Ideally I would like to be able to add on an autogenerated number onto the front-end ID, but then account for ID’s with the same initial string (like ‘btnAccept’ or ‘btnRemove’) regarless of their additional numeric suffixes. Is that possible at all?

Solution

ASP.NET Webforms auto generates button IDs for each control, unless you set it to

 ClientIDMode="Static"

If you’d like to make sure the IDs are unique, set

 ClientIDMode="AutoID" //this is the default setting for all webform controls, and autogenerates an ID

So the issue is probably coming from somewhere else, you can check it yourselves in the inspector of your browser.

Answered By – Zee

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *