 |
BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3
 | This is the forum to discuss the Wrox book Beginning ASP.NET 3.5: In C# and VB by Imar Spaanjaars; ISBN: 9780470187593 |
|
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|
|

April 25th, 2009, 11:05 AM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
|
|
GridView, HyperLinkField, and UrlEncode
Imar,
I ran into a problem on my project. I am working my way through it, but that work will likely be short circuited with your help.
I used a HyperLink field in a GridView for the name of a server in my application. It worked well until I came across a server with the characters "#3" in the name. I figured out that the "#" was seen as a delimiter and the HyperLink field is not sufficient.
The code for the GridView columns is:
Code:
<Columns>
<asp:BoundField DataField="ServerID" DataFormatString="{0:d}" HeaderText="Server ID"
ReadOnly="True" />
<asp:HyperLinkField DataNavigateUrlFields="ServerID,ServerName,IPAddress,ReleasePoint"
DataNavigateUrlFormatString="Detail.aspx?ServerID={0}&ServerName={1}&IPAddress={2}&ReleasePoint={3}"
DataTextField="ServerName" HeaderText="Server" />
<asp:BoundField DataField="IPAddress" HeaderText="IP Address" ReadOnly="True" />
<asp:BoundField DataField="ReleasePoint" HeaderText="Release Point" ReadOnly="True" />
</Columns>
I think I need to change the HyperLink field into a TemplateField using UrlEncode in the Markup view, and then use UrlUnEncode in the code behind with the extraction of the query string.
Am I on the right path?
Thanks.
Thomas
|
|

April 25th, 2009, 12:04 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Yes, that would be a good way to do it. Alternatively, you can embed the UrlEncode call directly in the template, without the need for a method in Code Behind. Something like this should work:
Code:
<ItemTemplate>
<asp:HyperLink ID="hyper" runat="server"
Text='<%# HttpUtility.UrlEncode(Eval("Text").ToString()) %>'
NavigateUrl='<%# HttpUtility.UrlEncode(Eval("Link").ToString()) %>' />
</ItemTemplate>
But do you really need to pass all this data to the next page? Doesn't your table have a primary key that can be used to uniquely identify a record? Or are all these fields part of a composite key?
Imar
|
|

April 25th, 2009, 12:51 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Thanks, Imar. I will try that a little later.
Here is what I am doing.
My application is for our support staff at my company. (They have been getting this support software from other sources that have become unavailable, and I am redirecting from pure C# support to ASP.NET and on an attempted fast learning curve.)
This was a kind of test, simple application for applying the principles which you have taught me. I have a main page, the Default.aspx that lists the servers we support. That data comes from a local database. I list all the servers, originally with the HyperLink field for the server name. When the user chooses a server I put the Server ID, name, IP address and Release Point in the query string and pass it to the next page. That page allows the users to query that server's transactions, and that page actually connects to the server in the field via a VPN.
Are you suggesting that I could / should do what I have described differently?
I appreciate your advice.
Thomas
|
|

April 25th, 2009, 12:56 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Is the ServerId unique throughout the Server table (just assuming you have a Server table here for the discussion)? If so, all you need to pass is the ServerId. Inside the receivig page, you can use the ServerId to connect to the database, and find all relevant information for the requested server. It may mean an additional call in the receiving page, but IMO it's worth it as it avoids cluttering up the query string and helps making your URLs and site in general more easy to manage and maintain (for example, if a server is renamed or gets a new IP address, old links using the ID, name and IP address are broken, whereas the link stays valid if you only use the ID).
Hope this helps,
Imar
|
|

April 25th, 2009, 01:16 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Imar,
The ServerID is unique but that is not what is used to connect to the actual data laden server. The IP address is used for that. Further, I display the Server ID, Server Name and Release Point, i.e., the data I am passing in the query string, at the top of the second page so that the user is reminded of what server he or she is working with.
If any of the pertinent data changes there is an application that runs nightly that updates the local database, the one from which I am getting the server details and connection information. It is unlikely that the user will ever be presented with a server with incorrect connection information, although they could find that they cannot connect to that server at that moment, but that is a rare occurrence.
Thomas
|
|

April 25th, 2009, 02:11 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
|
The ServerID is unique but that is not what is used to connect to the actual data laden server.
|
Yes, that makes sense. But nothing stops you from querying the relevant data in Detail.aspx based on the ServerId. E.g.:
Code:
int serverId = Convert.ToInt32(Request.QueryString.Get("ServerId"));
string sql = "SELECT IPAddress, Name FROM Server WHERE Id = {0}";
// Setup an ADO object or whatever it is you use to connect to the database (like an SqlDataSource), execute the SQL and query the relevant data.
This way, you only need to pass the server ID and query the rest of the data in Detail.aspx.
Just a thought; your idea would work as well, but has a few disadvantgaes as I mentioned earlier.
Cheers,
Imar
|
|

April 27th, 2009, 01:38 PM
|
|
Authorized User
|
|
Join Date: Mar 2009
Posts: 64
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Imar,
I did it the simpler way, i.e., passed the easy data then queried the database on the Details.aspx page for the Server Name, as you suggested. I agree that is easier, for me now.
I want to try using the ItemTemplate, but at the moment cannot quite see how to implement it. If I encode the URL in the markup of the Default.aspx I need to unencode it in the Details.aspx. Further, my query string has (originally) 4 parameters (ServerID, ServerName, IPAddress, ReleasePoint) that I want to pass, but only need to encode the ServerName.
Thomas
|
|

April 28th, 2009, 04:28 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
So is it working now? Or do you still need help? I don't see a question mark in your latest post, so maybe it's all working?
Imar
|
|
 |