user1875119
user1875119

Reputation: 1

About dynamically created radio button in c#

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";
    }

Upvotes: 0

Views: 4231

Answers (2)

Walt Ritscher
Walt Ritscher

Reputation: 7037

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();
    }

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460108

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;
}

Upvotes: 2

Related Questions