-2

I have a button which shows "click me!" on it. I want to see after clicking, it shows "Do not click on me!" permanently.

I get the below error message in buttonname_click() function and I do not know how to resolve it (I have spent so much time on it).

The name 'buttonname' does not exist in the current context

I am attaching Search.xaml and Search.xaml.cs in this question.

<sdk:Page   
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"   
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
xmlns:local="clr-namespace:TMTemplate"
xmlns:mocal="Clr-namespace:DynamicNavigation"
DataContext="{Binding RelativeSource={RelativeSource self}}"
xmlns:ContactForm="clr-namespace:ContactForm"
mc:Ignorable="d"
x:Class="TMTemplate.Search" 
Title="Search"
d:DesignWidth="640" d:DesignHeight="480" Height="530" Width="700">
<Grid x:Name="LayoutRoot" >
    <StackPanel Margin="50,45,25,45">
        <Button Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0"  Height="93" Width="518"></Button>
    </StackPanel>
</Grid>

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;
using System.Windows.Media.Imaging;

namespace TMTemplate
{
    public partial class Search : Page
    {
        public Search() { 
         InitializeComponent();
        }
        private void buttonname_Click(object sender, RoutedEventArgs e)
        {
         buttonname.Content = "Do not click on me!";
        }   
    }
}
Steve
  • 6,334
  • 4
  • 39
  • 67
Nikh
  • 27
  • 6
  • I was the one who asked that yesterday and still can not proceed – Nikh Jan 31 '14 at 19:43
  • Don't repost the same question. If you have information to add then edit the original. – paparazzo Jan 31 '14 at 20:05
  • This question appears to be off-topic because it does not show a basic level of understanding of programming. – Ian Ringrose Feb 08 '14 at 10:52
  • Hi lan Ringrose, You have a professional understanding of programming which is very good But I would like to let you know I am doing my masters and I have been programming in the past 9 years. I am new to Silverlight and faced this issue. I do not know if this is a bug or not but it does not work and this is what it is... Thank you very much – Nikh Feb 10 '14 at 20:48

2 Answers2

0

You could try this instead:

    private void buttonname_Click(object sender, RoutedEventArgs e)
    {
     ((Button)sender).Content = "Do not click on me!";
    } 

But, to run your code as is, you need to specify the name with x:Name instead of Name:

<Button x:Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0"  Height="93" Width="518"></Button>
Joe Brunscheon
  • 1,949
  • 20
  • 21
0
<Button x:Name="buttonname" Content="click me!" Click="buttonname_Click" Margin= "0,100,0,0"  Height="93" Width="518"></Button>

private void buttonname_Click(object sender, RoutedEventArgs e)
    {
     this.buttonname.Content = "Do not click on me!";
    } 
shuttle87
  • 15,466
  • 11
  • 77
  • 106
Alamgir
  • 686
  • 8
  • 14