views:

1425

answers:

3

Hi,

I have a page that displays a list of users. each user has an ID and a HyperlinkButton to watch more details about the user.

When pressing the HyperlinkButton, I would like to navigate to another page (called UserDetails) and somehow read the ID of the user that was pressed.

How can I do that?

Thanks, Ronny

+1  A: 

What about put ID in query string like so.

<HyperlinkButton 
  x:Name="btn" /**other properties**/
  NavigateUri="http://www.yoururl.com/details.aspx?ID=1234"&gt;
</HyperlinkButton>

in Details.aspx you can put ID in initParams property of silverlight object

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
  <param name="initParams" value='<%= GetID() %>' />
</object>

in Details.aspx.cs , code behind of Details.aspx, you fill the initParams like so

public string GetID(){
   return string.Format("ID={0}", Request.QueryString[0]);
}

then, you can read the ID from your silverlight application startup

    private void Application_Startup(object sender, StartupEventArgs e)
    {
       int ID = Convert.ToInt32(e.InitParams["ID"]);
    }
Anwar Chandra
Thanks for your replay.But is it possible to stay in the same aspx file?And use the relative path such as "NavigateUri="/UserDetails" + the ID
Ronny
Of course it is possible. But why do you need to navigate and pass data to the same page if you can pass the data without navigating? can't you?
Anwar Chandra
I do need to navigate between the two pages, the first is the page and that shows the list of the users (UsersList.xaml) and the second is the one that shows a specific user details (UserDetails.xaml).I Found a nice solution, but I would like to hear something that is more elegant solution.
Ronny
+2  A: 

I Found a nice solution, but I would like to hear something that is more elegant.

Within the UriMapper section I have added another UriMapping:

<uriMapper:UriMapping Uri="/UserDetails/{UserId}" MappedUri=/Views/UserDetails.xaml"/>

By doing so, all navigation in the format of "/UserDetails/XXX will be navigated to same page, UserDetails.xaml.

So now my HyperlinkButton is generated with a NavigateUri with the needed format:

NavigateUri="/UserDetails/1234"

Now, on the UserDetails.xaml page, in the OnNavigatedTo method, I can parse the Uri parameter (e.Uri) and load the User details accordingly.

Ronny
No need to parse the Uri. Change the MappedUri to "/Views/UserDetails.aspx?id={UserId}" next you'll be able to use NavigationContext.QueryString["id"] to get the value
TimothyP
A: 

you can use NavigationContext to get data from query string.. try this:

<HyperlinkButton NavigateUri="/UserDetails?userId=123" />    

and than in the navigated page something like this

string customerId = this.NavigationContext.QueryString["customerid"];
pori