Solved

Using .EVAL()  on navigateurl property in gridview control

Posted on 2010-08-13
14
1,103 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

739 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