Event Handler not firing for Silverlight page

  • Thread starter Thread starter Alex. O. Koranteng
  • Start date Start date
A

Alex. O. Koranteng

The button event handler for XAML page not firing for Textbox field, but it
fires for the date selected value for calendar. I have listed code file code
file below. Any suggestions will be appreciated.

<UserControl
xmlns:basics="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" x:Class="HelloSilverLight.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="Azure" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition
Height="220"/>
<RowDefinition
Height="40"/>
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="75" />
<ColumnDefinition Width="325" />

</Grid.ColumnDefinitions>


<TextBlock x:Name="name1" Text="Name:" Grid.Row="0"
Grid.Column="0"></TextBlock>

<TextBlock Text="Date:" Grid.Row="1" Grid.Column="0"></TextBlock>
<TextBlock x:Name="message1" Text="Message:" Grid.Row="2"
Grid.Column="0" Grid.ColumnSpan="2"></TextBlock>
<TextBox Text="Your Name" Width="150" HorizontalAlignment="Left"
Grid.Row="0" Grid.Column="1"></TextBox>
<StackPanel Grid.Column="1" Grid.Row="1" Orientation="Vertical" >
<basics:Calendar SelectionMode="SingleDate"
HorizontalAlignment="Left" x:Name="cal1"></basics:Calendar>
<Button Click="okButton_Click" x:Name="okButton" Width="75"
Height="25" HorizontalAlignment="Left" Content="OK" ></Button>
</StackPanel>

</Grid>
</UserControl>

Also below is my code behind for the page.XAML.cs flle.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace HelloSilverLight
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}

private void okButton_Click(object sender, RoutedEventArgs e)
{
string dateString;
if (cal1.SelectedDate == null)
{
dateString = "<date not selected>";
}
else
{
dateString = cal1.SelectedDate.ToString();
}
string s = message1.Text = "Hi " + name1.Text + "\n" +
"Selected Date: " + dateString;

}

private void Button_Click(object sender, RoutedEventArgs e)
{

}
}
}
 
Hi Alex,
The button event handler for XAML page not firing for Textbox field, but it
fires for the date selected value for calendar.

Is my understanding correct that you want to output below after clicking
the ok button?

Hi [user input in the TextBox goes here]
Selected Date: [user selected date goes here]

The problem is whatever you input in the TextBox, it always shows:

Hi Name:
Selected Date: [user selected date goes here]

Right? If so the problem is you're referring to the TextBlock instead of
the TextBox in the code behind. Please try this:

xaml:

<TextBox x:Name="TextBox1" Text="Your Name" Width="150"
HorizontalAlignment="Left"
Grid.Row="0" Grid.Column="1"></TextBox>

xaml.cs
private void okButton_Click(object sender, RoutedEventArgs e)
{
string dateString;
if (cal1.SelectedDate == null)
{
dateString = "<date not selected>";
}
else
{
dateString = cal1.SelectedDate.ToString();
}
string s = message1.Text = "Hi " + TextBox1.Text + "\n" +
"Selected Date: " + dateString;

}

In xaml give the TextBox a name and refer to it in code behind by its name.

If my understanding is wrong please clarify the problem.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Allen,

Thanks for the code suggestion. I have implemented them in my project but
still not getting the right output. I am sending a zipped file to your in box
for you to take a look at.

Allen Chen said:
Hi Alex,
The button event handler for XAML page not firing for Textbox field, but it
fires for the date selected value for calendar.

Is my understanding correct that you want to output below after clicking
the ok button?

Hi [user input in the TextBox goes here]
Selected Date: [user selected date goes here]

The problem is whatever you input in the TextBox, it always shows:

Hi Name:
Selected Date: [user selected date goes here]

Right? If so the problem is you're referring to the TextBlock instead of
the TextBox in the code behind. Please try this:

xaml:

<TextBox x:Name="TextBox1" Text="Your Name" Width="150"
HorizontalAlignment="Left"
Grid.Row="0" Grid.Column="1"></TextBox>

xaml.cs
private void okButton_Click(object sender, RoutedEventArgs e)
{
string dateString;
if (cal1.SelectedDate == null)
{
dateString = "<date not selected>";
}
else
{
dateString = cal1.SelectedDate.ToString();
}
string s = message1.Text = "Hi " + TextBox1.Text + "\n" +
"Selected Date: " + dateString;

}

In xaml give the TextBox a name and refer to it in code behind by its name.

If my understanding is wrong please clarify the problem.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Alex,
Thanks for the code suggestion. I have implemented them in my project but
still not getting the right output. I am sending a zipped file to your in box
for you to take a look at.


Thanks for your project. You're adding a new TextBox whose name is
TextBox1. However the original TextBox still exists there and it's the
TextBox whose Text is changed by you. The new TextBox1 is not shown on the
screen therefore you cannot change it's Text.

Please remove the following TextBox from the xaml to make it work:

<TextBox Text="Your Name" Width="150" HorizontalAlignment="Left"
Grid.Row="0" Grid.Column="1"></TextBox>

Regards,
Allen Chen
Microsoft Online Support
 
Allen,

It works now. I see what I am doing wrong. I was confused on what a TextBox
and TextBlock elements are. I caould not have done it without you. Thanks a
lot. I am starting to get up to speed on Databinding and have realized I need
to get comfortable with WCF. Could you point me to some good links on these
topics
 
Hi Alex,
I am starting to get up to speed on Databinding and have realized I need
to get comfortable with WCF. Could you point me to some good links on these
topics

Do you have further questions?

Regards,
Allen Chen
Microsoft Online Support
 
Back
Top