Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-11-21
4
Medium Priority
?
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

705 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