Solved

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

Posted on 2013-11-21
4
283 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

627 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