Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

auto-update label in edit template of gridview where dropdownlist selected value changes

Posted on 2015-02-09
7
Medium Priority
?
243 Views
Last Modified: 2015-02-21
Hi Experts,

I have a gridview with two dropdownlists - DDLotStartEdit & DDLotEndEdit. These are populated programmatically - with a bit of a fiddle to get the selected time showing as the selected value in each:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                Label durationLBL = (Label)e.Row.FindControl("lblDuration") as Label;
                Label lblstart = (Label)e.Row.FindControl("lblStart");
                Label lblend = (Label)e.Row.FindControl("lblEnd");
                DropDownList ddList = (DropDownList)e.Row.FindControl("DDLotStartEdit");
                DropDownList ddList2 = (DropDownList)e.Row.FindControl("DDLotEndEdit");
                ddList.Items.Add("--select a time--");
                ddList2.Items.Add("--select a time--");
                List<string> times = new List<string>();
                for (int hour = 0; hour < 24; hour++)
                    for (int minute = 0; minute < 60; minute++)
                        if (minute % 15 == 0)
                            times.Add(string.Format("{0:00}:{1:00}", hour, minute));

                foreach (string child in times)
                {

                    ddList.Items.Add(child);
                    ddList2.Items.Add(child);

                }

                ddList.SelectedValue = lblstart.Text.ToString().Substring(0, 5);
                ddList2.SelectedValue = lblend.Text.ToString().Substring(0, 5);
...}}

Open in new window


The two labels holding the selected start and end times are populated as follows:
 protected void DDLotStartEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddList = sender as DropDownList;
        DropDownList ddList2 = sender as DropDownList;
        string start = ddList.SelectedValue.ToString().Substring(0, 5);
        string end = ddList2.SelectedValue.ToString().Substring(0, 5);
        

        Label lblstart = ddList.NamingContainer.FindControl("lblstart") as Label;
        lblstart.Text = ddList.SelectedValue.ToString().Substring(0, 5);
       
    }

    protected void DDLotEndEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddList = sender as DropDownList;
        DropDownList ddList2 = sender as DropDownList;
        string start = ddList.SelectedValue.ToString().Substring(0, 5);
        string end = ddList2.SelectedValue.ToString().Substring(0, 5);
      
        Label lblend = ddList2.NamingContainer.FindControl("lblend") as Label;
        lblend.Text = ddList2.SelectedValue.ToString().Substring(0, 5);
        
    }

Open in new window


In a third column I have a label displaying the duration in hh:mm between the selected start and end times; I am trying to get this to update within the edit view of the grid i.e. as the user edits one or other of the selections in the dropdownlists to change the start or end time I want to recalculate the duration and change the label to reflect it before the user hits update.
I think this will need to be done with JavaScript which I'm less familiar with, can anyone advise me ? The relevant columns of the gridview are below for ref:
 
<asp:TemplateField HeaderText="Start Time">
                                                <ItemTemplate>
                                                    <%# Eval("otStart") %>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                     <asp:Label ID="lblStart" runat="server" Text='<%# Bind("otStart")%>' Visible ="false"></asp:Label>
                                                    <asp:DropDownList ID="DDLotStartEdit" Width="120px" OnSelectedIndexChanged="DDLotStartEdit_SelectedIndexChanged" AutoPostBack="true" runat="server">
                                                    </asp:DropDownList>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="End Time">
                                                <ItemTemplate>
                                                    <%# Eval("otEnd") %>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                     <asp:Label ID="lblEnd" runat="server" Text='<%# Bind("otEnd")%>' Visible ="false"></asp:Label>
                                                    <asp:DropDownList ID="DDLotEndEdit" OnSelectedIndexChanged="DDLotEndEdit_SelectedIndexChanged" AutoPostBack="true"
                                                        Width="120px" runat="server">
                                                    </asp:DropDownList>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Duration">
                                                <ItemTemplate> 
                                                    
                                                   <%# Eval("otDuration")%>  
                                                   <%--<asp:Label ID="lblDuration" runat="server" Text='<%# Bind("otDuration") %>' Visible="true"></asp:Label>--%>
                                                </ItemTemplate>
                                                 <EditItemTemplate>
                                                  <asp:Label ID="lblDuration" runat="server" Text='<%# Bind("otDuration") %>' Visible="true"></asp:Label>
                                                </EditItemTemplate>
                                            </asp:TemplateField> 

Open in new window

0
Comment
Question by:forsters
  • 5
  • 2
7 Comments
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 40603401
If you are referring to this control

 <asp:DropDownList ID="DDLotStartEdit" Width="120px" OnSelectedIndexChanged="DDLotStartEdit_SelectedIndexChanged" AutoPostBack="true" runat="server">

then, I don't believe that you need any Javascript.  You could do this work in the DDLotStartEdit_SelectedIndexChanged event handler.
0
 

Author Comment

by:forsters
ID: 40603566
Hi,

Thanks for your comment, well that was my first thought, but I wasn't sure how and ran into problems accessing the controls etc.

If you can help me to get that working I'd really appreciate it.
0
 

Author Comment

by:forsters
ID: 40603615
This is where I got to it appears to work as desired except that the calculation is always 00:00 (minor detail)

 
protected void DDLotStartEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddList = sender as DropDownList;
        DropDownList ddList2 = sender as DropDownList;
        string start = ddList.SelectedValue.ToString().Substring(0, 5);
        string end = ddList2.SelectedValue.ToString().Substring(0, 5);

        Label durationLBL = ddList.NamingContainer.FindControl("lblDuration") as Label;
        Label lblstart = ddList.NamingContainer.FindControl("lblstart") as Label;
        lblstart.Text = ddList.SelectedValue.ToString().Substring(0, 5);
        
        TimeSpan durationSpan = new TimeSpan((Convert.ToDateTime(end).Ticks - Convert.ToDateTime(start).Ticks));
        durationLBL.Text = durationSpan.ToString().Substring(0, 5);
    }

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 40603821
I really like it when I can help someone help themselves...that is my goal in life--answering your own questions
0
 

Author Comment

by:forsters
ID: 40605195
ok, but I am still no further forward because the code I posted miscalculates or isn't picking up the correct values to do the math....
0
 

Assisted Solution

by:forsters
forsters earned 0 total points
ID: 40611924
solved by my colleague:

protected void DDLotStartEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddList = sender as DropDownList;
        DropDownList ddList2 = ddList.NamingContainer.FindControl("DDLotEndEdit") as DropDownList;
        string start = ddList.SelectedValue.ToString().Substring(0, 5);
        string end = ddList2.SelectedValue.ToString().Substring(0, 5);

        Label durationLBL = ddList.NamingContainer.FindControl("lblDuration") as Label;
        Label lblstart = ddList.NamingContainer.FindControl("lblstart") as Label;
        lblstart.Text = ddList.SelectedValue.ToString().Substring(0, 5);
        
        TimeSpan durationSpan = new TimeSpan((Convert.ToDateTime(end).Ticks - Convert.ToDateTime(start).Ticks));
        durationLBL.Text = durationSpan.ToString().Substring(0, 5);
    }

    protected void DDLotEndEdit_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddList2 = sender as DropDownList;
        DropDownList ddList = ddList2.NamingContainer.FindControl("DDLotStartEdit") as DropDownList;
        string end = ddList.SelectedValue.ToString().Substring(0, 5);
        string start = ddList2.SelectedValue.ToString().Substring(0, 5);
        
        Label durationLBL = ddList.NamingContainer.FindControl("lblDuration") as Label;
        Label lblend = ddList2.NamingContainer.FindControl("lblend") as Label;
        lblend.Text = ddList2.SelectedValue.ToString().Substring(0, 5);

        TimeSpan durationSpan = new TimeSpan((Convert.ToDateTime(ddList2.SelectedValue).Ticks - Convert.ToDateTime(ddList.SelectedValue).Ticks));
        durationLBL.Text = durationSpan.ToString().Substring(0, 5);

    }

Open in new window

0
 

Author Closing Comment

by:forsters
ID: 40622811
I have selected my own comment as part of the solution as it shows the final working code
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

971 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