Saturday, March 31, 2012

trim a URL link through datagrid

Hi,

Is there anyway I can trim this URL?

The Output is something like this:http://www.test.com/default.aspx?siteid=1&pagetitle=this_is_a_test and I want to get rid of everything past "?" so it just showshttp://www.test.com/default.aspx

This is the code i am using to get the URL through a datagrid in C#: 'Navigate='<%# Databinder.Eval(Container.DataItem, "LinkPath") %>' How can I trim this URL in the backend?

Thanks for anyone that can help me out.

Hi

Hope this helps

//Create a method calledstring GetTrimmedURL(string CompleteURL){ string TrimmedURL = String.Empty; string str = CompleteURL;int intIndexOfchar = str.IndexOf("?"); TrimmedURL = str.substring(intIndexOfchar, (str.Length() - intIndexOfchar));return TrimmedURL;}

Call this method at the place where you are doing the data binding. Say something like

'Navigate='<%# GetTrimmedURL(Databinder.Eval(Container.DataItem, "LinkPath")) %>'

Please note that this trims from the first instance of question mark though.

Hope this helps

Note: Pls watch out for small syntactical errors. Did this on a notepad.

VJ


Hi

You can do the following to strip the querystring off the URL:

Uri linkPath =new Uri(YOUROBJECTHERE.LinkPath);string newLinkPath = linkPath.Scheme + "://" + linkPath.Host + linkPath.AbsolutePath;

You could either do this is in the class you are databinding to, or perhaps in an OnItemDataBound event handler on your DataGrid, depending on your preference.

Hope this helps.

Seb

0 comments:

Post a Comment