How to put the focus to a TextBox in OnNavigatedTo event? - c#

In Windows 8, when a page is navigated, I need to put the focus to a specific TextBox.
I used:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}
But it does not work. However, if I call myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard); from a button click event, it works fine.
How can I set the focus to a TextBox when the page is loaded?

Try calling myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
with this.Dispatcher
A code sample:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
Dispatcher.RunAsync(
CoreDispatcherPriority.Normal,
() => myControl.Focus(FocusState.Keyboard));
}

Try this:
myControl.Focus(Windows.UI.Xaml.FocusState.Keyboard);
and set this control(mycontrol) as the first control in your XAML code.

On Windows Phone 8.1 add a Loaded event to the page and call the focus in there instead of OnNavigatedTo event
private void Page_Loaded(object sender, RoutedEventArgs e)
{
SearchTextBox.Focus(FocusState.Keyboard);
}

I did it by adding it in the Loaded event of the TextBox:
xaml:
<TextBox x:Name="SearchTextBox"
Loaded="SearchTextBox_Loaded"/>
in the code behind .cs:
private void SearchTextBox_Loaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
SearchTextBox.Focus(Windows.UI.Xaml.FocusState.Keyboard);
}

Related

Trigger a click sender in another method

I have the following codes that is the button on and off:
private void OnActivity_Click(object sender, RoutedEventArgs e)
{
if ((sender as Button).Content.Equals("On"))
{
(sender as Button).Content = "Off";
}
else
{
(sender as Button).Content = "On";
}
}
I have another method named protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
How can I call the sender as Button of OnActivity_Click into another method ? I need to trigger On and Off then On again in the navigation method. Someone help me pls ! Thanks !
Inside OnNavigatedTo you can use the button reference. For example:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
OnActivity_Click(this.btn1, null);
}
Your button here has the name "btn1" (set it in xaml).
Give name to the button in xaml like x:name="btn"
and then use it in OnNavigatedTo method like this,
btn.content="On";

How do I make a button show the listbox when clicked?

I am making a form where if a button is clicked it will go to my list box and run the functions I have in there though I am a bit confused on how to make it realise when the button has been clicked and for the listbox to work. Here is my code >_>
private void button1_Click(object sender, EventArgs e)
{
}
public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Ping.PlayConsole();
}
You just need:
private void button1_Click(object sender, EventArgs e)
{
Ping.PlayConsole();
}
It's ok to call the same function under different handlers.
Try this :
private void button1_Click(object sender, EventArgs e)
{
ListBox_SelectedIndexChanged(sender,e);
}
Good Luck !!
private void button1_Click(object sender, EventArgs e)
{
ListBox.Focus();
Ping.PlayConsole();
}
Both event handlers share the same signature void (object , EventArgs), so they are call-compatible.
If you are connecting the event visually using the form designer:
Go to the Property Inspector's Events pane and instead of double-clicking it to create an event handler stub for button1.Click, click the dropdown box icon that appears on the right side. Visual Studio will show all the event handlers present in the form that have a compatible signature, you should be able to choose ListBox_SelectIndexChanged for the button1.Click handler. They will be sharing the same handler.
If you are connecting the handler by code then this should work too:
ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);

Run the button event handler before page_load

I have a table with all the objects I have in my db. I load them in my Page_Load function. I have a text field and a button that when clicking the button, I want the handler of that click to put a new object with the name written in the text field in the db.
Now, I want that what happens after the click is that the page loads again with the new item in the table. The problem is that the button event handler is run after the Page_Load function.
A solution to this would be to use IsPostBack in the Page_Load or use the pre load function. A problem is that if I would have 3 different buttons, I would have to differ between them there instead of having 3 different convenient functions.
Any solutions that don't have this problem?
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userId"] == null)
Response.Redirect("Login.aspx");
// LOAD DATA FROM DB
}
protected void CreateObject(object sender, EventArgs e)
{
// SAVE THE NEW OBJECT
}
Maybe you should try loading your data during PreRender instead of Load
protected void Page_Load(object sender, EventArgs e)
{
this.PreRender += Page_PreRender
if (Session["userId"] == null)
Response.Redirect("Login.aspx");
}
protected bool reloadNeeded {get; set;}
protected void CreateObject(object sender, EventArgs e)
{
// SAVE THE NEW OBJECT
reloadNeeded = true;
}
protected void Page_PreRender(object sender, EventArgs e)
{
if(reloadNeeded || !IsPostBack)
// LOAD DATA FROM DB
}
You can check the event target and do what you need then:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
string eventTarget = Page.Request.Params["__EVENTTARGET"];
if(whatever)
{
//do your logic here
}
}
}
Get control name in Page_Load event which make the post back
Use the Page_PreRenderComplete event to retrieve your table. That way your page will always have the most recent data available after all user events have fired.
Why don't you move what you have in the click event into a new method. Then call that method as the first line in your page load?
An old question but I faced the same problem in my C#/ASP.NET Website with master/content pages: a click on a link on the master page should change a query parameter for a gridview on the content page. As you stated the button click event is handled after Page_Load. But it is handled before Page_LoadComplete (see the information about ASP.NET Page Life Cycle), so you can change the page content there.
In my case I solved the problem by setting a session variable in the click event in the master page, getting this variable in the Page_LoadComplete event in the content page and doing databind based on that.
Master page:
<asp:LinkButton ID="Btn1" runat="server" OnCommand="LinkCommand" CommandArgument="1" >Action 1</asp:LinkButton>
<asp:LinkButton ID="Btn2" runat="server" OnCommand="LinkCommand" CommandArgument="2" >Action 2</asp:LinkButton>
protected void LinkCommand(object sender, CommandEventArgs e)
{
HttpContext.Current.Session["BindInfo", e.CommandArgument.ToString());
}
Content page:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string BindInfo = HttpContext.Current.Session["BindInfo"].ToString();
YourBindDataFunction(BindInfo);
}

How to handle the Before Close Event in a WPF Browser Application?

I am just starting with a WPF Browser application.
I would like to handle the Before Close Event of the browser, so how would i handle that event?
If i understood your question correctly you can:
add your BrowserControl tosome Window and handle Window's Closing event.
or you can try variations of Unloaded event(this one all
FrameworkElement objects have).
Update
Sorry, just came back..
Example:
yourControl.Unloaded += (s, e) =>
{
// some code here
e.Handled = true;
};
I found this solution.
In the App.xaml.cs file:
private void Application_Onexit(object sender, ExitEventArgs e)
{
//write your code in here!
}
In the App.xaml file:
<Application x:Class="(yourclasss)"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="Application_Startup"
Exit="Application_Onexit"
ShutdownMode="OnLastWindowClose"
StartupUri="startup.xaml">
So basically the ShutdownMode property needs to be set in order to make it work.
Then add a event in app.xaml.cs:
public static event EventHandler UnloadPageWorkaround;
public void Application_Onexit(object sender, ExitEventArgs e)
{
UnloadPageWorkaround.Invoke(null, null);
}
Then on the relevant page:
private void Page_Loaded(object sender, RoutedEventArgs e)
{
WPFBrowser.App.UnloadPageWorkaround += new EventHandler(DoMySpecialPageCleanupStuff);
}
void DoMySpecialPageCleanupStuff(object sender, EventArgs e)
{
//do cleanup
}
The only problem with this is, you can't stop the application from exiting.
Hope it helps.

Calling click event of a button serverside

How can I click or load the click() event of a button in codebehind?
I already tried
btn.Click();
but this gives an error. I am using ASP.NET
I will assume that you have a button called Button1
and that you have double clicked it to create an event handler.
To simulate the button click in code, you simply
call the event handler:
Button1_Click(object sender, EventArgs e).
e.g. in your Page Load Event
protected void Page_Load(object sender, EventArgs e)
{
//This simulates the button click from within your code.
Button1_Click(Button1, EventArgs.Empty);
}
protected void Button1_Click(object sender, EventArgs e)
{
//Do some stuff in the button click event handler.
}
If you don't need the context provided by the sender and eventargs then you can just refactor to create a method (eg DoStuff()) and make your event handler just call that. Then when you want to call the same functionality from elsewhere you can just call DoStuff(). It really depends on whether you want to actually simulate a click or not. If you want to simulate a click then other methods are better.
Do you wish to call all the event handlers attached to the button, or just one?
If just one, call the handler:
btn.btn_Click(btn, new EventArgs());
If all of them, trigger the event:
var tmpEvent = btn.Click;
if (tmpEvent != null)
tmpEvent(btn, new EventArgs());
Try this:
YourButton_Click(sender, e);
Edit try this:
protected void Page_Init(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
}
void Button1_Click(object sender, EventArgs e)
{
}
in the .cs page
Just call the buttons click function.
http://forums.asp.net/t/1178012.aspx
Instead of trying to call the event just call the function that event uses.
Example: btn.Click() calls
btn_Click(object sender, EventArgs e)
so you should call the function btn_Click(btn,new
EventArgs());
It's been a long time ago, but here is my solution
You can use:
btn.PerformClick();
protected void Page_Load(object sender, EventArgs e)
{
functionName();
}
protected void btn_Click(object sender, EventArgs e)
{
functionName();
}
public void functionName()
{
//function code goes here, and call this function where ever you want
}
try this one.......
YourButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
This works for me.

Categories

Resources