We help IT Professionals succeed at work.

how can i have a DetailsView convert Latitude & longitude from floating point (eg: 123.5) to degrees.minutes (eg: 123.30)?

mikepj
mikepj asked
on
427 Views
Last Modified: 2008-01-09
haw can i have a DetailsView convert Latitude & longitude from floating point (eg: 123.5) to degrees.minutes (eg: 123.30)?  i have only a fairly basic understanding of ASP.NET.  

the conversion from 123.5 to 123.30 (and "back" for saving) is not a problem.  

my question is about intercepting that data before it is displayed so we can change it.  for saving, we need to intercept it again and convert it back to floating point for saving...

thank you for your help!
Comment
Watch Question

Within your Itemcreated event, find the control with the Lat/Long and run your conversion.  For more on this check http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemcreated(VS.80).aspx

You can change the value during the ItemUpdating event of the DetailsView  
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.detailsview.itemupdating(VS.80).aspx

Author

Commented:
thank you for your reply; that's a good step forward.  something that's holding me up is that i dislike referring to fields by ordinal number.  i'd like to find them by name but i haven't found a way to get the DetailsView to let me find the field by name.  do you know the answer to that?
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
DetailsView.ItemCreated Event only occurs when a record is created in a DetailsView control.  it fires but is blank (it's as though it's firing before data binding).
My Bad,  This is an example for a string in your databind item ... the DetailsView

      <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" OnDataBound="DetailsView1_DataBound" AutoGenerateRows="False" DataKeyNames="weaponTypeID">
        <Fields>
          <asp:BoundField DataField="weaponTypeID" HeaderText="weaponTypeID" InsertVisible="False" ReadOnly="True" SortExpression="weaponTypeID" />
          <asp:TemplateField HeaderText="weaponType" SortExpression="weaponType">
            <EditItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("weaponType") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
              <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("weaponType") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
              <asp:Label ID="WeaponType" runat="server" Text='<%# Bind("weaponType") %>'></asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
        </Fields>

  protected void DetailsView1_DataBound(object sender, EventArgs e)
  {
    //DataRowView drv = (DataRowView)DetailsView1.DataItem;
    // Conversion here on  drv.Row["WeaponType"]
    Label lbl = (Label)DetailsView1.FindControl("WeaponType");
    lbl.Text = "Fred";
  }

Author

Commented:
thank you for your help!  it works; can you review my work?

although this works, it's longer & more convoluted than i had expected.  am i doing this correctly?  what i'm wondering about is the use of edtLatitude1and edtLatitude2, etc.  seems like i might be over-doing this a bit.  your opinion.

      protected void dvBlock_DataBound(object sender, EventArgs e)
      {
         DataRowView drv = (DataRowView) dvBlock.DataItem;

         double dLat;

         if (drv.Row.IsNull("Latitude"))
            dLat = 0;
         else
            dLat = (double)drv["Latitude"];

         string sLat = Lib.JRPUtils.LatLongToStr(dLat);

         Label lbl = (Label)dvBlock.FindControl("lblLatitude");
         if (lbl != null)
            lbl.Text = sLat;

         TextBox edt = (TextBox)dvBlock.FindControl("edtLatitude1");
         if (edt != null)
            edt.Text = sLat;

         edt = (TextBox)dvBlock.FindControl("edtLatitude2");
         if (edt != null)
            edt.Text = sLat;

         double dLong;

         if (drv.Row.IsNull("Longitude"))
            dLong = 0;
         else
            dLong = (double)drv["Longitude"];

         string sLong = Lib.JRPUtils.LatLongToStr(dLong);

         lbl = (Label)dvBlock.FindControl("lblLongitude");
         if (lbl != null)
            lbl.Text = sLong;

         edt = (TextBox)dvBlock.FindControl("edtLongitude1");
         if (edt != null)
            edt.Text = sLong;

         edt = (TextBox)dvBlock.FindControl("edtLongitude2");
         if (edt != null)
            edt.Text = sLong;
      }

      protected void dvBlock_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
      {
         e.NewValues["Latitude"] = Lib.JRPUtils.StrToLatLong((string) e.NewValues["Latitude"]);
         e.NewValues["Longitude"] = Lib.JRPUtils.StrToLatLong((string) e.NewValues["Longitude"]);
      }



   public class JRPUtils
   {
      public static string LatLongToStr(double d)
      {
         if (d > 0)
         {
            int iDeg = (int)d;
            int iMin = (int)((d - iDeg) * 60);

            return string.Format("{0}.{1:00}", iDeg.ToString(), iMin);
         }
         else
         {
            return "";
         }
      }

      public static double StrToLatLong(string s)
      {
         s.Trim();

         if (s == "") return 0;

         double d = double.Parse(s);

         int iDeg = (int) d;
         double dMin = (d - iDeg) / 0.60;

         return iDeg + dMin;
      }
   }



  <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label><asp:DetailsView ID="dvBlock" runat="server" AutoGenerateRows="False" CellPadding="4"
      DataKeyNames="BlockID,OwnerID" DataSourceID="dsBlock" DefaultMode="Edit" ForeColor="#333333"
      GridLines="None" Height="50px" Width="125px" OnDataBound="dvBlock_DataBound" OnItemUpdating="dvBlock_ItemUpdating" >
      <Fields>
         <asp:TemplateField HeaderText="Latitude" SortExpression="Latitude">
            <EditItemTemplate>
               <asp:TextBox ID="edtLatitude1" runat="server" Text='<%# Bind("Latitude") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
               <asp:TextBox ID="edtLatitude2" runat="server" Text='<%# Bind("Latitude") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ControlStyle Width="50px" />
            <ItemTemplate>
               <asp:Label ID="lblLatitude" runat="server" Text='<%# Bind("Latitude") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
         <asp:TemplateField HeaderText="Longitude" SortExpression="Longitude">
            <EditItemTemplate>
               <asp:TextBox ID="edtLongitude1" runat="server" Text='<%# Bind("Longitude") %>'></asp:TextBox>
            </EditItemTemplate>
            <InsertItemTemplate>
               <asp:TextBox ID="edtLongitude2" runat="server" Text='<%# Bind("Longitude") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ControlStyle Width="50px" />
            <ItemTemplate>
               <asp:Label ID="lblLongitude" runat="server" Text='<%# Bind("Longitude") %>'></asp:Label>
            </ItemTemplate>
         </asp:TemplateField>
      </Fields>
   </asp:DetailsView>

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.