Silverlight passing parameters between pages using HyperlinkButton

杀马特。学长 韩版系。学妹 提交于 2019-12-08 16:27:27

问题


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:


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.




回答2:


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"];



回答3:


What about put ID in query string like so.

<HyperlinkButton 
  x:Name="btn" /**other properties**/
  NavigateUri="http://www.yoururl.com/details.aspx?ID=1234">
</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"]);
    }


来源:https://stackoverflow.com/questions/1692553/silverlight-passing-parameters-between-pages-using-hyperlinkbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!