Solved

Update Link doesn't always display in GridView when Edit link is clicked

Posted on 2013-11-21
4
277 Views
Last Modified: 2013-12-15
I have a GridView in asp.net (c#) that has the following definition:

            <asp:GridView ID="gvSchedules" runat="server" CellPadding="5" OnRowCreated="gvSchedules_RowCreated"
                OnPageIndexChanging="gvSchedules_PageIndexChanging" OnRowCancelingEdit="gvSchedules_RowCancelingEdit"
                OnRowEditing="gvSchedules_RowEditing" OnRowUpdating="gvSchedules_RowUpdating" OnSelectedIndexChanged="gvSchedules_SelectedIndexChanged"
                AlternatingRowStyle-BackColor="#0099CC" AlternatingRowStyle-BorderColor="Black" 
                AlternatingRowStyle-BorderStyle="Solid" AlternatingRowStyle-ForeColor="White" 
                AlternatingRowStyle-HorizontalAlign="Left" AlternatingRowStyle-VerticalAlign="Middle" 
                BorderStyle="Solid" HeaderStyle-BackColor="Silver" BorderColor="Black" 
                HeaderStyle-BorderColor="Black" HeaderStyle-BorderStyle="Solid" 
                HeaderStyle-HorizontalAlign="Center" HeaderStyle-VerticalAlign="Middle" 
                HorizontalAlign="Left" RowStyle-BorderColor="Black" RowStyle-BorderStyle="Solid" 
                RowStyle-HorizontalAlign="Left" RowStyle-VerticalAlign="Middle" 
                AutoGenerateColumns="False" 
                EmptyDataText="No Schedules Found" AllowPaging="True" AutoGenerateSelectButton="True" AutoGenerateEditButton="True"
                SelectedRowStyle-BorderColor="Red" SelectedRowStyle-BorderStyle="Solid" SelectedRowStyle-BackColor="#993399">
                <AlternatingRowStyle HorizontalAlign="Left" VerticalAlign="Middle" BackColor="#0099CC" BorderColor="Black" 
                    BorderStyle="Solid" ForeColor="White"></AlternatingRowStyle>
                <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" BackColor="Silver" BorderColor="Black" 
                    BorderStyle="Solid"></HeaderStyle>
                <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" BorderColor="Black" BorderStyle="Solid"></RowStyle>
                <Columns>
                    <asp:BoundField DataField="MasterScheduleID" HeaderText="Schedule ID" ReadOnly="true" />
                    <asp:BoundField DataField="DivisionID" HeaderText="Division ID" ReadOnly="true" />
                    <asp:BoundField DataField="Division" HeaderText="Division" ReadOnly="true" />
                    <asp:TemplateField HeaderText="Active">
                        <EditItemTemplate>
                            <asp:DropDownList ID="ddlStatus" runat="server">
                                <asp:ListItem Value="Y">ACTIVE</asp:ListItem>
                                <asp:ListItem Value="N">INACTIVE</asp:ListItem>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblActiveFlag" runat="server" Text='<%# Bind("ActiveStatusFlag") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Schedule">
                        <EditItemTemplate>
                            <asp:TextBox ID="tbEdit" runat="server" ToolTip="Schedule" Text='<%# Bind("Description") %>'
                                CssClass="ScheduleTextFormat" MaxLength="100"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="tbScheduleRequiredFieldValidator2" runat="server" 
                                ErrorMessage="Schedule is required" BackColor="Red" Font-Bold="True" ForeColor="White"
                                ControlToValidate="tbEdit" Display="Dynamic" validationgroup="Info"
                                Text="*"></asp:RequiredFieldValidator>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="lblSchedule" runat="server" Text='<%# Bind("Description") %>'></asp:Label>                       
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="MonStartTime" HeaderText="Mon Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="MonEndTime" HeaderText="Mon End Time" ReadOnly="true" />
                    <asp:BoundField DataField="MonHours" HeaderText="Mon Hours" ReadOnly="true" />
                    <asp:BoundField DataField="TueStartTime" HeaderText="Tue Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="TueEndTime" HeaderText="Tue End Time" ReadOnly="true" />
                    <asp:BoundField DataField="TueHours" HeaderText="Tue Hours" ReadOnly="true" />
                    <asp:BoundField DataField="WedStartTime" HeaderText="Wed Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="WedEndTime" HeaderText="Wed End Time" ReadOnly="true" />
                    <asp:BoundField DataField="WedHours" HeaderText="Wed Hours" ReadOnly="true" />
                    <asp:BoundField DataField="ThuStartTime" HeaderText="Thu Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="ThuEndTime" HeaderText="Thu End Time" ReadOnly="true" />
                    <asp:BoundField DataField="ThuHours" HeaderText="Thu Hours" ReadOnly="true" />
                    <asp:BoundField DataField="FriStartTime" HeaderText="Fri Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="FriEndTime" HeaderText="Fri End Time" ReadOnly="true" />
                    <asp:BoundField DataField="FriHours" HeaderText="Fri Hours" ReadOnly="true" />
                    <asp:BoundField DataField="SatStartTime" HeaderText="Sat Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="SatEndTime" HeaderText="Sat End Time" ReadOnly="true" />
                    <asp:BoundField DataField="SatHours" HeaderText="Sat Hours" ReadOnly="true" />
                    <asp:BoundField DataField="SunStartTime" HeaderText="Sun Start Time" ReadOnly="true" />
                    <asp:BoundField DataField="SunEndTime" HeaderText="Sun End Time" ReadOnly="true" />
                    <asp:BoundField DataField="SunHours" HeaderText="Sun Hours" ReadOnly="true" />
                </Columns>
            </asp:GridView>
        </ContentTemplate>

Open in new window


The problem is on most rows, when Select is clicked, the Edit link displays and when Edit is clicked the Update and Cancel links display.  However, on some rows the Update link doesn't display (only the Cancel link) when Edit is clicked.  It appears to be data related but I can't figure out what in the data is causing the problem.

Here is the code for the RowEditing event:

        protected void gvSchedules_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Master.resultsMessage = null;
            Master.showResults = false;

            try
            {
                // Cancel the paging operation if the user attempts to edit another record while the 
                // gridview control is in edit mode.
                if (gvSchedules.SelectedIndex == -1 || gvSchedules.SelectedIndex != e.NewEditIndex)
                {
                    // Use the Cancel property to cancel 
                    e.Cancel = true;

                    // Display an error message
                    Master.resultsMessage = "You must select the record before trying to edit.";
                    Master.showResults = true;
                    return;
                }

                // Cancel the paging operation if the user attempts to edit another record while the 
                // gridview control is in edit mode.
                if (gvSchedules.EditIndex != -1)
                {
                    // Use the Cancel property to cancel 
                    e.Cancel = true;

                    // Display an error message
                    Master.resultsMessage = "Please finish updating the current record.";
                    Master.showResults = true;
                    return;
                }

                // Unhide Select link so Update link will display
                gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[0].Visible = true;
                gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[1].Visible = true;
                gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[2].Visible = true;

                // get current schedule value
                Label lblReason = gvSchedules.Rows[e.NewEditIndex].FindControl("lblSchedule") as Label;
                String currentReason = lblReason.Text;

                // get currently selected Status value
                Label label = gvSchedules.Rows[e.NewEditIndex].FindControl("lblActiveFlag") as Label;
                String currentValue = label.Text;

                // switch grid to edit mode and re-bind
                gvSchedules.EditIndex = e.NewEditIndex;
                gvSchedules.DataSource = dtMasterSchedule;
                gvSchedules.DataBind();

                // grab the DropDownList and find the corresponding option
                DropDownList ddl = gvSchedules.Rows[e.NewEditIndex].FindControl("ddlStatus") as DropDownList;
                ListItem item = ddl.Items.FindByValue(currentValue);

                // set option selected if found
                if (item != null)
                    item.Selected = true;
                
                // Enable Edit Schedule Hours textboxes
                tbEditMonStart.Enabled = true;
                tbEditMonEnd.Enabled = true;
                tbEditMonHours.Enabled = true;
                tbEditTueStart.Enabled = true;
                tbEditTueEnd.Enabled = true;
                tbEditTueHours.Enabled = true;
                tbEditWedStart.Enabled = true;
                tbEditWedEnd.Enabled = true;
                tbEditWedHours.Enabled = true;
                tbEditThuStart.Enabled = true;
                tbEditThuEnd.Enabled = true;
                tbEditThuHours.Enabled = true;
                tbEditFriStart.Enabled = true;
                tbEditFriEnd.Enabled = true;
                tbEditFriHours.Enabled = true;
                tbEditSatStart.Enabled = true;
                tbEditSatEnd.Enabled = true;
                tbEditSatHours.Enabled = true;
                tbEditSunStart.Enabled = true;
                tbEditSunEnd.Enabled = true;
                tbEditSunHours.Enabled = true;

                // Hide page row
//                gvSchedules.PagerSettings.Visible = false;

                // Update Schedule List Panel
                udpScheduleList.Update();
                udpEditSchedules.Update();
            }
            catch (Exception exp)
            {
                string host = HttpContext.Current.Request.Url.Host.ToLower();
                if (!host.Contains("d-conn"))
                {
                    IntranetSupport iSupport = new IntranetSupport();
                    string returnvalue = "";
                    returnvalue = iSupport.ReportError(exp, Convert.ToInt32(Session["EmployeeProfileID"]), HttpContext.Current.Request.Url.AbsolutePath, "", "");
                    if (returnvalue != "")
                        Response.Redirect(returnvalue);
                }
                else
                {
                    throw exp;
                }
            }
        }

Open in new window


Any help is pointing me in the right direction for solving this is greatly appreciated!
0
Comment
Question by:dyarosh
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 39668866
What is this code used for?

 
 // Unhide Select link so Update link will display
                gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[0].Visible = true;
                gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[1].Visible = true;
                gvSchedules.Rows[e.NewEditIndex].Cells[0].Controls[2].Visible = true;

Open in new window

0
 

Author Comment

by:dyarosh
ID: 39709003
That code was used in an attempt to display the Update and Cancel links.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 39709207
You shouldn't need that code, if you use the GridView.ShowEditButton and ShowCancelButton properties.

CommandField.ShowEditButton Property
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.commandfield.showeditbutton(v=vs.100).aspx
0
 

Author Closing Comment

by:dyarosh
ID: 39720469
Thank you.
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question