How to access buttons in diffrent pages in windowsphone apps - c#

I have a question like this, I have two pages and have one button in each page, like this mainpage.xaml and second page.xaml, my button in mainpage was disabled with this code: <Button Name="one" IsEnabled="False" Content="Button" />.
Now I want to enable this button with another button that placed in second page.xaml, how I can access the button in mainpage.xaml and make it enable?
I can't access one.isenabled=true in second page, what can I do?

This is not the correct way of doing that.
You can have a global varible to read value from & set enabled/disabled accordingly, send data through URIs (Navigation Contexts) etc. There can be many other solutions too.
At the worst case, take reference variable of the button in code-behind of MainPage.xaml (public static Button b=one;) & access it from another page (MainPage.b.isEnabled=true;). Still, I don't recommend this.

Related

Previous page instance not maintained after navigation in prism

I am using the latest version of Prism.MVVM in Xamarin.Forms. In this, if I try to navigate to second page from the first page, the first page is initialized once again. i.e., the constructor of the first page is called once again.
For example, I am having Page1.xaml and Page2.xaml pages with their respective view models(those will be created and registered automatically while creating in prism).
I navigating to Page2 from Page1 like below,
NavigationAsync("Navigation/Page1/Page2")
While navigating, Page1.xaml's constructor is called so that the page is created newly which lead I could not able to maintain the Page1.xaml instance. Also, please note that Page1.xaml is a Master-Details page.
Is this a behavior in Prism? If so how can I overcome this?
Thanks in advance.
Navigating away from a XAML page destroys it in UWP. You can preserve a page’s state data (and avoid re-construction) by adding a single line in a XAML page’s tag:
NavigationCacheMode="Required"
Does it work the same in Xamarin?

What is the best way to manipulate controls that are within a sub-frame? (UWP)

I'm not sure of what is the best approach for manipulating controls that are within a sub-frame of a Page. Look:
<Page x:Name="ParentPage">
<!-- ParentPage contains a button. When the button is clicked I would like to select all the items in a ListView that is in the InnerPage Page. -->
<Frame x:Name="SubFrame">
<Page x:Name="InnerPage" />
</Frame>
</Page>
Now InnerPage is actually loaded via C# code because there are actually several different Pages that are loaded into the SubFrame Frame, and so it is hard to do something like the example below. All of the Pages that are loaded into the SubFrame Frame contain the exact same ListView, though.
public sealed partial class ParentPage : Page
{
public ParentPage()
{
InnerPage _InnerPage = SubFrame.Content as InnerPage;
_InnerPage.SelectAllListViewItems();
}
}
Not too clear why you use different inner pages with the same ListView in the SubFrame and what the real effect you want to implement. But you can try to add some different pages in your project, then operate the page in the page's code behind, it would be more easily to manage the pages.
In the parent page, you can use the Frame.Navigate Method to load your child page.
SubFrame.Navigate(typeof(ChildPage));

Reusable SplitView pane?

I have created a simple SplitView and I'm wondering if I can reuse the code in my <SplitView.Pane>? It's pretty simple, just a few buttons, but I don't want to have to include the same code over and over again on my different pages.
Or is there a way to template the whole SplitView and just modify the <SplitView.Content>on every page? I'd prefer to be able to be able to have the button handlers be global too so that I don't have the same 3 buttons coded in each page .cs file.
What would be the best way to do this?
EDIT: This is for a UWP Windows 10 app.
You can use your own user control to reuse the content of splitview.pane.
UserControl class
Here is an example, my code.. 'FutaLocation' is my usercontrol.
<SplitView x:Name="Splitter" Grid.Row="1"
PanePlacement="Left"
DisplayMode="Inline"
OpenPaneLength="500"
PaneClosed="Splitter_PaneClosed"
PaneClosing="Splitter_PaneClosing"
>
<SplitView.Pane>
<futaviewcontrols:FutaLocation x:Name="futaLocation"/>
</SplitView.Pane>
<Pivot x:Name="directPivot"
If you can make a server control out of it, you can resuse and configure however you like. If this is not possible, you can make a code snippet and then place on page and alter as needed.

Is making an asp:Button control invisible enough to be sure users won't be able to click it?

I'm making a simple website that lists files from a certain folder. If the user has admin rights, the user can delete files by clicking the "Delete" button.
In my .aspx file, I have the following code:
<asp:Button runat="server" Text="Delete" OnCommand="FileList_Delete"
CommandArgument='<%#Eval("FilePath")%>' Visible='<%CurrentUserIsAdmin()%>' />
So the button will not be rendered if CurrentUserIsAdmin() returns false.
The button is rendered like this:
<input type="submit" name="ctl00$ctl00$MainContent$LocalMainContent$FileList$ctrl0$ctl17" value="Delete" />
My question is: Can I be sure that this method is safe against a known-code attack if the user modifies the webpage client-side aiming to click this invisible button? Or do I have to take precautions in the code-behind and verify the user's rights in the button-clicked event?
Yes, setting a button's Visible property to false is enough to prevent its Click and Command events from being raised, as long as you don't turn off the default WebForms security features.
You can easily test this by temporarily adding an always-visible <input> element to your .aspx with the same name as the rendered <asp:Button>:
<input type="submit"
name="ctl00$ctl00$MainContent$LocalMainContent$FileList$ctrl0$ctl17"
value="Fake Delete" />
Click the fake Delete button when the real Delete button is invisible. You should get an "Invalid postback or callback argument. Event validation is enabled..." exception.
Important notes:
Don't set a button's Visible property to false within an if (!IsPostBack) block because it's possible for an attacker to bypass that check. See this answer for more information.
ASP.NET event validation must be enabled (which it is by default). So don't turn it off by adding EnableEventValidation="False" to the #Page directive or <pages enableEventValidation="false" /> to Web.config.
Never ever ever disable view state validation by adding EnableViewStateMac="False" to the #Page directive or <pages enableViewStateMac="false" /> to Web.config. This would allow an attacker to tamper with the hidden __EVENTVALIDATION field and do other nasty things.
If you choose a derive a custom Button server control from the standard Button control, make sure you add the [SupportsEventValidation] attribute to the derived class.
If you choose to create a custom Button server control from scratch, call RegisterForEventValidation and ValidateEvent in the appropriate places.
They simply won't see the button or even 'recieve' it. Your server will not generate any button code sent to the person.
You have to think of it this way. The user never sees any asp code or is able to process it. They only receive html. You can further ensure this by looking at the html and seeing what has been generated.
So in that regard you are safe.
My question is: can I be sure that this method is safe against known-code attack if user modifies the webpage client-side aiming to click this invisible button? Or I have to make precautions in CodeBehind and verify user rights in button clicked event?
I personally would also put another piece of code in the click event. Verifying that click comes from the user who is authorized to click that button.
What you could also do is to add a button from code behind as this (Assuming you are putting this button into a panel called pnlButtons):
Button btnDeleteList = new Button();
btnDeleteList.Text = "Delete List";
btnDeleteList.Click += btnDeleteList_Click;
pnlButtons.Controls.Add(btnDeleteList);
In other words, if user is Admin - add a button, if user is not an admin - do not add. In this case you do not have to play around with visibility.
hope this helps.

return PartialView to a popup windows at another view

I have an Index.aspx with a button inside which that button will call a controller, doing some logic and returning to a PartialView control - let's named it PopUpPartialView.ascx (as a popup). So to make it clear, the popup windows(PopUpPartialView) actually stays ON the top of Index.aspx when user clicks on the button.
In PopUpPartialView.ascx, there is another button, that returns say a GenerateList and now the problem is - how do I pass the thing back to the same popup windows in PopUpPartialView.ascx on the top of Index.aspx as it was before? How should my controller codes look like?
Here's what I have on the return:
return PartialView("PopUpPartialView", GenerateList);
this clearly NOT working as what I want, because it doesn't point back to Index page. I was thinking perhaps to use ajax so that I could stay on that popup ascx page. Confused~~ Someone please guide me.
Thanks.
My advice is to use a plugin which handles all the popup plumbing for you.
My poison of choice is jqModal.
It's very easy to work with - essentially a hidden container on the page, and you can load contents in there either on the initial render, or on a click event via AJAX.
So in your example, you could handle the button event click, show the dialog and load the contents of your partial view into the hidden container.

Categories