1
Reply

Calculate value for specific label in DataList

xuan xuan

xuan xuan

May 19 2010 11:46 PM
3.2k
hiex! i've been figuring out this problem for days....seriously need help...

This function i m working on is to calculate the shortest distance between the delivery man and the destination by using GPS coordinates. After clicking a button, named as btnShortestDist, it will retrieve all delivery man details on a page. I can't create that many sql statement to call all delivery men's details out. Thus, I used DataList and databind it with the database on sql server. 

The problem is, for each delivery man, I need to calculate the shortest distance. I dragged a label to the DataList's item template. This label, named as DistanceLabel, should use the distance calculation method in the c# page and give an output of the calculated distance. How do I do this for all the delivery men? Is it possible?


My C# distance calculation method: 


public static double CalculateDistance(double lon1, double lat1, double lon2, double lat2) { 
        const double R = 3956; 
        const double degreesToRadians = Math.PI / 180; 

        //convert from fractional degrees (GPS) to radians 
        lon1 *= degreesToRadians; 
        lat1 *= degreesToRadians; 
        lon2 *= degreesToRadians; 
        lat2 *= degreesToRadians; 
    
        double dlon = lon2 - lon1; 
        double dlat = lat2 - lat1; 
        double a = Math.Pow(Math.Sin(dlat / 2), 2) + Math.Cos(lat1) * Math.Cos(lat2) * Math.Pow(Math.Sin(dlon / 2), 2); 
        double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); 
        double d = R * c; 
    
        return d;
    }

The DataList code that i did for now:


            <asp:DataList ID="DataList1" runat="server" DataKeyField="StaffID" 
                DataSourceID="SqlDataSource1" RepeatColumns="3" 
                RepeatDirection="Horizontal">
                <ItemTemplate>
                    StaffID:
                    <asp:Label ID="StaffIDLabel" runat="server" Text='<%# Eval("StaffID") %>' />
                    <br />
                    StaffName:
                    <asp:Label ID="StaffNameLabel" runat="server" Text='<%# Eval("StaffName") %>' />
                    <br />
                    Distance:
                    <asp:Label ID="DistanceLabel" runat="server" Text="Label"></asp:Label>
                    <br />
                    <br />
                </ItemTemplate>
            </asp:DataList>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:VCdeliveryconnectionString %>" 
                SelectCommand="SELECT [StaffID], [StaffName] FROM [Staff] WHERE ([Role] = @Role)">
                <SelectParameters>
                    <asp:Parameter DefaultValue="DeliveryMan" Name="Role" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>

Answers (1)