0

I'm trying to develop a Windows 8 Metro app where i needed to create an arbitrary number of radio buttons, but the Checked event handler is not firing up.

I read in some post that I've to enable AutoPostBack.

Please let me know in which namespace it is? Also i found that it is in System.Web.UI.Webcontrols, but i'm unable to include that namespace..

I'm using visual studio 2012 ultimate if that helps

RadioButton rad=new RadioButton();
            rad.HorizontalAlignment = HorizontalAlignment.Left;
            rad.VerticalAlignment = VerticalAlignment.Top;
            rad.Margin = new Thickness(1100, x, 0, 0);
            rad.Width = 35;
            rad.Height = 30;
            rad.GroupName = "group1";
            rad.IsEnabled = true;
            rad.Checked += new RoutedEventHandler(radbtn);
            gridit.Children.Add(rad[i]);

void radbtn(object obj, RoutedEventArgs e)
    {
        edit_del_tb.Text = "Testing";
    }
Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35
user1875119
  • 1
  • 1
  • 2
  • 2
    where is your code that you tried? – Manish Mishra May 15 '13 at 12:36
  • 1
    You clearly haven't searched before asking... googling "autopostback" provides good answers in the first results... – Laurent S. May 15 '13 at 12:37
  • RadioButton is indeed in System.Web.UI.WebControls.RadioButton, once you've created an instance of that type server side, you can turn its AutoPostBack property to true – vc 74 May 15 '13 at 12:37
  • Error 2 'Windows.UI.Xaml.Controls.RadioButton' does not contain a definition for 'AutoPostBack' and no extension method
    'AutoPostBack' accepting a first argument of type 'Windows.UI.Xaml.Controls.RadioButton' could be found (are you missing a
    using directive or an assembly reference?) I'm getting this error
    – user1875119 May 15 '13 at 12:41
  • Based on your error, I'm guessing that you are NOT building a web page. AutoPostBack is for ASP.NET. – CM Kanode May 15 '13 at 12:48
  • It's for building a metro app. My problem is the checked event handler is not firing up when i click on radio button – user1875119 May 15 '13 at 12:54
  • Please edit your question and add the appropriate tags. – CM Kanode May 15 '13 at 13:26
  • If rad is a RadioButton, then what is rad[i]??? – Filip Skakun May 15 '13 at 17:24
  • Sorry my bad, I edited the question. – user1875119 May 15 '13 at 19:17

2 Answers2

2

AutoPostBack is not in a namespace, it's a property of CheckBox because a RadioButton inherits from CheckBox.

You also have to ensure that dynamic controls are recreated on every postback with the same ID as before and in Page_Load at the latest.

How to: Add Controls to an ASP.NET Web Page Programmatically.

Register the CheckedChanged event programmatically:

RadioButton btn = new RadioButton();
btn.AutoPostBack = true;
btn.CheckedChanged += this.RadioButton_CheckedChanged;
Panel1.Controls.Add(btn);

in this class:

private void RadioButton_CheckedChanged(Object sender, EventArgs e)
{
    // get the reference to the RadioButton that caused the CheckedChanged-event
    RadioButton btn = (RadioButton) sender;
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

First, you need a better understanding of what UI technology you are working with.

.NET has many UI frameworks:

  • Winforms
  • WPF
  • Silverlight
  • ASP.NET webforms
  • ASP.NET MVC
  • Windows Phone,
  • Windows Store apps.

Most of these UI frameworks have RadioButton controls. They are different classes, and have different properties and behaviors.

Postback is part of the ASP.NET webforms world and is not what you are looking for!

Make sure when you are looking for help to use the correct framework. (On MSDN there is usually a dropdown at the top of the page.)

Working Example

Looks like your problem is that you are adding an array of radiobuttons to the Grid, not the RadioButton itself. Its a bit hard to tell, because you didn't include your XAML or all of your C# code.

Here is some code that does work.

XAML

 <Grid 
        Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
      <RowDefinition Height='30' />
      <RowDefinition Height='1*' />
    </Grid.RowDefinitions>
    <TextBlock x:Name='edit_del_tb' />
    <Grid Grid.Row='1'
          x:Name='gridit'></Grid>
    </Grid>

C# code

   public MainPage() {
      this.InitializeComponent();
      for (int i = 0; i < 4; i++)
      {
        RadioButton rad = new RadioButton();
        rad.HorizontalAlignment = HorizontalAlignment.Left;
        rad.VerticalAlignment = VerticalAlignment.Top;
        rad.Margin = new Thickness(100, i * 40, 0, 0);
        rad.Width = 350;
        rad.Height = 30;
        rad.GroupName = "group1";
        rad.IsEnabled = true;
        rad.Content = "Button " + i;
        rad.Checked += new RoutedEventHandler(radbtn);
        gridit.Children.Add(rad);
      }

    }
    void radbtn(object obj, RoutedEventArgs e) {
      edit_del_tb.Text = (obj as RadioButton).Content.ToString();
    }
Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35