Click a button on a website using C# - html

I'm developing an windowsphone 8 application.
My goal is to click on a certain button on a website from my code behind (C#).
Is there anyway to achieve this?
Example:
I have a button named 'MyButton" on Mainpage. Then in the event handler:
private void BtnMyButton_Click(object sender, EventArgs e)
{
//click button 'Search' on Google
}

You could register a Javascript function to call the click event.
In codebehind:
protected void BtnMyButton_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\"> document.getElementById('yourButtonID').onclick(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "searchClick", script);
}
Or if you're using jQuery:
protected void BtnMyButton_Click(object sender, EventArgs e)
{
string script = "<script type=\"text/javascript\"> $('yourButtonID').Click(); </script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "searchClick", script);
}

Related

My button on click is not working

I have a registration page in that page I have a text box for mobile number and a button in my designing page but when I click on the submit button its not fire in the coding page where the method button_click define.
my designing page:
<asp:Button ID="btnprocced1" class="btn btn-primary jk" runat="server"
Text="procced" OnClick="btnprocced1_Click" />
code behind:
protected void btnprocced1_Click(object sender, EventArgs e)
{
...
}
Note: I'm using web user control so my question is How to generate
buttonclick event in .ascx page.
If you want to fire button click event using .ascx page then you'll first have to add following code in your code behind.
.ascx Code.
public partial class Test : System.Web.UI.UserControl
{
public event EventHandler buttonClick;
protected void Button1_Click(object sender, EventArgs e)
{
buttonClick(sender, e);
}
}
Then you can subscribe the event buttonClick at webpage to display the different information .
.aspx.cs Code.
public partial class Test: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
UserControlID.buttonClick+=new EventHandler(UserControlID_buttonClick);
}
protected void UserControlID_buttonClick(object sender, EventArgs e)
{
Response.Write("Hey, ButtonClick Is Working Now.");
}
}
It would help to identify button click whenever it's triggered. Hope it helps.

Back button to go back in WebBrowser WP8 youtube site

I made a WebBrowser and it works except the back button in a youtube site. Youtube have a redirect to mobile version and this cause a loop
this code not works
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.InvokeScript("eval", "history.go(-1)" );
}
I know if it is a redirect (302) in this event?
webBrowser_Navigated(object sender, NavigationEventArgs e)
In the WebBrowserControl documentation you can find all available methods and events.
It is not very pretty but you should try that:
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
webBrowser.GoBack();
e.Cancel = true;
}
void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if (e.Uri.ToString().Contains("YouTubeUriYouGetAlwaysRedirectedTo"))
{
// Do some stuff here
e.Cancel = true;
}
}

datepicker in popup directly open when I click a button?

As described i input a datepicker in my xaml file
when i run the page ,datepicker just show like this:
then I have to tap the datepicker to enter the select page like this :
Now
I need to directly open the fullscreen datepicker select page when I click a button
the address give a way that I can just Navigate to the select page,
but I don't know how ?
I'm the poster.
i find a solution myself
Override DatePicker class with our custom DatePickerCustom class. Create new class "DatePickerCustom.cs"
public class DatePickerCustom : DatePicker
{
public void ClickTemplateButton()
{
Button btn = (GetTemplateChild("DateTimeButton") as Button);
ButtonAutomationPeer peer = new ButtonAutomationPeer(btn);
IInvokeProvider provider = (peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider);
provider.Invoke();
}
}
then in the mainpage.xaml.cs
private DatePickerCustom datePicker;
// Constructor
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// create datePicker programmatically
if (this.datePicker == null)
{
this.datePicker = new DatePickerCustom();
this.datePicker.IsTabStop = false;
this.datePicker.MaxHeight = 0;
this.datePicker.ValueChanged += new EventHandler<DateTimeValueChangedEventArgs>(datePicker_ValueChanged);
LayoutRoot.Children.Add(this.datePicker);
}
}
void datePicker_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
{
// now we may use got value from datePicker
TextBlock1.Text = this.datePicker.ValueString;
}
so that when do an action like tap or click, the fullscreen datepicker page will be shown
private void button1_Click(object sender, RoutedEventArgs e)
{
this.datePicker.ClickTemplateButton();
}
ps: timepicker can also do the same thing
ps2:here is the details
#Mario Galván
hope it help u

How to change Background image of button on mouse click, created "Dynamically" ? - Windows Phone

I've created buttons dynamically, now unable to handle events on it, there is no usefull link on internet..
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
...
void button_MouseLeave(object sender, EventArgs e)
{
}
void button_MouseEnter(object sender, EventArgs e)
{
}
This code is not working ...
MouseEnter and MouseLeave are respectively triggered when the mouse pointer hovers on and out of an object. The event you're searching for is Click.
Other than that, your code should work:
button.Click += new RoutedEventHandler(button_Click);
void button_Click(object sender, RoutedEventArgs e)
{
// Whatever
}
Try the MouseLeftButtonDown and MouseLeftButtonUp events. Change the image to the new one in MouseLeftButtonDown and change it back with MouseLeftButtonUp.
So simple, just put this code as it is.
button.MouseEnter += new EventHandler(button_MouseEnter);
button.MouseLeave += new EventHandler(button_MouseLeave);
void button_MouseLeave(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camBlue.png", UriKind.Relative));
Button1.Background = brush;
}
void button_MouseEnter(object sender, EventArgs e)
{
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri("/Images/camRed.png", UriKind.Relative));
Button1.Background = brush;
}

caliburn winrt usercontrol and capturing custom event

I made my own slider as user control with some custom properties and one custom event. Everything works fine, but recently I start using Caliburn Micro, and I don't know how to capture my custom event.
Previously I used:
<my:RadialSlider x:Name="slider" WedgeAngle="270" ..... AngleChanged="slider_AngleChanged" />
and
public void slider_AngleChanged(object sender, ValueChangedEventArgs e)
{
.... something ....
}
Now, in Caliburn project I tried:
<my:RadialSlider x:Name="slider" WedgeAngle="270" ..... cal:Message.Attach="[Event AngleChanged] = [Action slider_AngleChanged($eventArgs)]" />
and in my ViewModel:
public void slider_AngleChanged(object sender, ValueChangedEventArgs e)
{
.... something ....
}
But, this doesn't work...
So, how to capture this event?
Slider UC code-behind:
public delegate void AngleChangedEventHandler(object sender, ValueChangedEventArgs e);
public sealed partial class RadialSlider : UserControl
{
public event AngleChangedEventHandler AngleChanged;
private void OnAngleChanged(ValueChangedEventArgs e)
{
if (AngleChanged != null)
AngleChanged(this, e);
}
public static readonly DependencyProperty WedgeAngleProperty = DependencyProperty.Register("WedgeAngle", typeof(double), typeof(RadialSlider), new PropertyMetadata((double)270, new PropertyChangedCallback(OnPropertyWedgeAngleChanged)));
public double WedgeAngle
{
get { return (double)this.GetValue(WedgeAngleProperty); }
set { this.SetValue(WedgeAngleProperty, value); }
}
private static void OnPropertyWedgeAngleChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
(sender as RadialSlider).UpdateControls();
if (e.NewValue != e.OldValue)
{
(sender as RadialSlider).OnAngleChanged(new ValueChangedEventArgs((double)e.OldValue, (double)e.NewValue));
}
}
}
You need to use a routed event. This has to do with how events bubble up the visual tree and how Caliburn.Micro attaches itself to them. Standard events should be avoided on controls or UI widgets in any tech using Xaml as the loose out on some pretty funky features (bubble up / down).