Solved

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

Posted on 2015-02-09
7
201 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 500 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now