Solved

Using .EVAL()  on navigateurl property in gridview control

Posted on 2010-08-13
14
1,097 Views
Last Modified: 2013-11-26
How would I write the EVAL expression in this block of code so that the "TrackingNum" fields url is generated depending on the "ShipMethod" field. There are 3 different Shipping methods "UPS","USPS" and "FDXSP".
<asp:GridView ID="GridView8" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2"
                            Width="868px" CellPadding="4" ForeColor="#333333" GridLines="None">
                            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                            <Columns>
                                <asp:HyperLinkField HeaderText="Order #" DataTextField="OrderNum" DataNavigateUrlFields="OrderNum"
                                    DataNavigateUrlFormatString="http://marketplaceadvisor.channeladvisor.com/en/order/order_detail.aspx?il=1&fOrderUID={0}"
                                    Target="_blank" />
                                <asp:HyperLinkField HeaderText="Tracking #" DataTextField="TrackingNum" DataNavigateUrlFields="TrackingNum"
                                    DataNavigateUrlFormatString="http://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=en_US&trackNums={0}&track.y=10&Requester=TRK_MOD&showMultipiece=N&detailNumber=undefined&WBPM_lid=homepage%2Fct1.html_pnl_trk"
                                    Target="_blank">
                                    <ItemStyle BackColor="#CCCCCC" Font-Size="X-Small" />
                                </asp:HyperLinkField>
                                <asp:BoundField DataField="ShipDate" HeaderText="ShipDate" SortExpression="ShipDate" />
                                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                                <asp:BoundField DataField="Cost" HeaderText="Cost" SortExpression="Cost" />
                                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
                                <asp:BoundField DataField="ShipService" HeaderText="ShipService" SortExpression="ShipService" />
                                <asp:BoundField DataField="ShipMethod" HeaderText="ShipMethod" SortExpression="ShipMethod" />
                            </Columns>
                            <EditRowStyle BackColor="#999999" />
                            
                        </asp:GridView>

Open in new window

0
Comment
Question by:frtools
  • 7
  • 6
14 Comments
 
LVL 16

Expert Comment

by:Stephan
ID: 33433886
I think the only way to do this, is creating a "ItemDataBound" event.
0
 
LVL 5

Assisted Solution

by:KiasChaos83
KiasChaos83 earned 250 total points
ID: 33434334
Hmm.. my solution has two steps.

1) In the code behind, add a public function:

public string GetShippingMethodDescription(string method) {
switch (method) {
case "UPS": return "hello"; break;
case "USPS": return "world; break;
case "FDXSP": return "blah"; break;
}
return "";
}

2) add the following to the aspx

<%# GetShippingMethodDescription(EVAL("ShipMethod")) %>

0
 

Author Comment

by:frtools
ID: 33505158
Sorry for the late response (vacation).

KiasChaos83, when I inserted the code there is an error - Databinding expressions are only supported on objects that have a Databinding event. System.Web.UI.WebControls.HyperLinkField does not have a DataBinding event.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 16

Expert Comment

by:Stephan
ID: 33508044
instead of using an HyperLinkField, you need to create a templatefield with an hyperlink in it, then add the code that KiasChaos83 has submitted.
0
 

Author Comment

by:frtools
ID: 33510891
I added this following code and get a parse error on the hyperlink tag.


 <asp:TemplateField HeaderText = "Tracking #" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#  etShippingMethodDescription(EVAL("ShipMethod","TrackingNum")) %>' />
                                </ItemTemplate>
                                </asp:TemplateField>

Open in new window

0
 
LVL 16

Expert Comment

by:Stephan
ID: 33511964

<asp:TemplateField HeaderText = "Tracking #" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#  GetShippingMethodDescription(Eval("ShipMethod")) %>' />
                                </ItemTemplate>
                                </asp:TemplateField>

Open in new window

0
 

Author Comment

by:frtools
ID: 33514990
Appreciate all the help.

It kind of works but I need to pass another eval object into my function how would I do that with an '&' or something else. Also the column does not contain a link.
Below is what I would suppose the eval statement would look like but the error says

<asp:TemplateField HeaderText = "Tracking #" >
                                <ItemTemplate>
                                   <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#  GetShippingMethodDescription(Eval("ShipMethod") & Eval("TrackingNum")) %>' />
                                </ItemTemplate>
                                </asp:TemplateField>

My function is as follows:

public string GetShippingMethodDescription(object method, object tracknum)
        {
           
            switch (method.ToString()) {
            case "UPS": return "http://wwwapps.ups.com/WebTracking/track?HTMLVersion=5.0&loc=en_US&trackNums="+tracknum.ToString()+"&track.y=10&Requester=TRK_MOD&showMultipiece=N&detailNumber=undefined&WBPM_lid=homepage%2Fct1.html_pnl_trk"; break;
            case "USPS": return "http://trkcnfrm1.smi.usps.com/PTSInternetWeb/InterLabelInquiry.do?CAMEFROM=OK&strOrigTrackNum=" + tracknum.ToString() ; break;
            case "FDXSP": return "";
            break;
            }
            return "";
        }
0
 

Author Comment

by:frtools
ID: 33515024
Sorry you can disregard the EVAL error. It will not display the hyperlink field and I would like it to be the tracking number that I pull from the database.
0
 
LVL 16

Expert Comment

by:Stephan
ID: 33515088
I know why the hyperlink isn't showing, because you did not define the text property of the hyperlink
0
 

Author Comment

by:frtools
ID: 33515171
Alright I do have an EVAL error on this, excuse my confusion.


Operator '&' cannot be applied to operands of type 'object' and 'object'

<asp:TemplateField HeaderText = "Tracking #" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="HyperLink1" runat="server" Text="TrackingNum" Target ="_blank" NavigateUrl='<%#  GetShippingMethodDescription(Eval("ShipMethod") & Eval("TrackingNum")) %>' />
                                </ItemTemplate>
                                </asp:TemplateField>
0
 
LVL 16

Expert Comment

by:Stephan
ID: 33515201
Use , instead of &
0
 

Author Comment

by:frtools
ID: 33515310
Thank you so much that did it. One last question how would I bind the tracking number from the database in the text field.
0
 
LVL 16

Accepted Solution

by:
Stephan earned 250 total points
ID: 33515323
<asp:TemplateField HeaderText = "Tracking #" >
                                <ItemTemplate>
                                    <asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Eval("TrackingNum") %>' Target ="_blank" NavigateUrl='<%#  GetShippingMethodDescription(Eval("ShipMethod"), Eval("TrackingNum")) %>' />
                                </ItemTemplate>
                                </asp:TemplateField>
0
 

Author Comment

by:frtools
ID: 33515372
Thank you so much.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

839 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