Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Dynamic created radio buttons wont execute event on click (c# .net )

Dynamic created radio buttons wont execute event on click (c# .net )

Scheduled Pinned Locked Moved C#
csharphtmllinqdesignsysadmin
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    salar1369
    wrote on last edited by
    #1

    hi I have a button named Button1 in my form, and a text box named TexBox1. I've written code that when I click the Button1, a Radio button gets created with it's own name: c#:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    
    namespace WebApplication7
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page\_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    ViewState\["Counter"\] = 0;
                }
            }
    
            protected void Button1\_Click(object sender, EventArgs e)
            {
    
                HtmlGenericControl div = new HtmlGenericControl("div");
                for (int i = 0; i < Convert.ToInt32(ViewState\["Counter"\]); i++)
                {
                    RadioButton rb = new RadioButton();
                    rb.GroupName = "GN1";
                    rb.ID = i.ToString();
                    rb.Text = "Button" + i.ToString();
                    rb.Checked = false;
                    rb.CheckedChanged += radioButton\_CheckedChanged;
                    div.Controls.Add(rb);
                    Panel1.Controls.Add(div);
    
                }
                ViewState\["Counter"\] = Convert.ToInt32(ViewState\["Counter"\]) + 1;
    
    
            }
            private void radioButton\_CheckedChanged(object sender, EventArgs e)
            {
                RadioButton btn = sender as RadioButton;
                TextBox1.Text = btn.Text;
            }
        }
    }
    

    .NET

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
    
    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
    
            <asp:Panel ID="Panel1" runat="server">
            </asp:Panel>
            <asp:Button ID="Button1" runat="server" OnClick="Button1\_Click" Text=&q
    
    Kornfeld Eliyahu PeterK G 2 Replies Last reply
    0
    • S salar1369

      hi I have a button named Button1 in my form, and a text box named TexBox1. I've written code that when I click the Button1, a Radio button gets created with it's own name: c#:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.UI;
      using System.Web.UI.HtmlControls;
      using System.Web.UI.WebControls;
      
      namespace WebApplication7
      {
          public partial class WebForm1 : System.Web.UI.Page
          {
              protected void Page\_Load(object sender, EventArgs e)
              {
                  if (!IsPostBack)
                  {
                      ViewState\["Counter"\] = 0;
                  }
              }
      
              protected void Button1\_Click(object sender, EventArgs e)
              {
      
                  HtmlGenericControl div = new HtmlGenericControl("div");
                  for (int i = 0; i < Convert.ToInt32(ViewState\["Counter"\]); i++)
                  {
                      RadioButton rb = new RadioButton();
                      rb.GroupName = "GN1";
                      rb.ID = i.ToString();
                      rb.Text = "Button" + i.ToString();
                      rb.Checked = false;
                      rb.CheckedChanged += radioButton\_CheckedChanged;
                      div.Controls.Add(rb);
                      Panel1.Controls.Add(div);
      
                  }
                  ViewState\["Counter"\] = Convert.ToInt32(ViewState\["Counter"\]) + 1;
      
      
              }
              private void radioButton\_CheckedChanged(object sender, EventArgs e)
              {
                  RadioButton btn = sender as RadioButton;
                  TextBox1.Text = btn.Text;
              }
          }
      }
      

      .NET

      <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
      
      <!DOCTYPE html>
      
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head runat="server">
          <title></title>
      </head>
      <body>
          <form id="form1" runat="server">
          <div>
      
              <asp:Panel ID="Panel1" runat="server">
              </asp:Panel>
              <asp:Button ID="Button1" runat="server" OnClick="Button1\_Click" Text=&q
      
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu PeterK Offline
      Kornfeld Eliyahu Peter
      wrote on last edited by
      #2

      As you radio buttons are dynamic there will not be exist on the next post-back. You have to recreate them every time you got a post-back, so you have to maintain some state that tells you if there was radio buttons. As now the only time you create the radio button is on Button.OnClick...

      I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

      "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

      S 1 Reply Last reply
      0
      • S salar1369

        hi I have a button named Button1 in my form, and a text box named TexBox1. I've written code that when I click the Button1, a Radio button gets created with it's own name: c#:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.HtmlControls;
        using System.Web.UI.WebControls;
        
        namespace WebApplication7
        {
            public partial class WebForm1 : System.Web.UI.Page
            {
                protected void Page\_Load(object sender, EventArgs e)
                {
                    if (!IsPostBack)
                    {
                        ViewState\["Counter"\] = 0;
                    }
                }
        
                protected void Button1\_Click(object sender, EventArgs e)
                {
        
                    HtmlGenericControl div = new HtmlGenericControl("div");
                    for (int i = 0; i < Convert.ToInt32(ViewState\["Counter"\]); i++)
                    {
                        RadioButton rb = new RadioButton();
                        rb.GroupName = "GN1";
                        rb.ID = i.ToString();
                        rb.Text = "Button" + i.ToString();
                        rb.Checked = false;
                        rb.CheckedChanged += radioButton\_CheckedChanged;
                        div.Controls.Add(rb);
                        Panel1.Controls.Add(div);
        
                    }
                    ViewState\["Counter"\] = Convert.ToInt32(ViewState\["Counter"\]) + 1;
        
        
                }
                private void radioButton\_CheckedChanged(object sender, EventArgs e)
                {
                    RadioButton btn = sender as RadioButton;
                    TextBox1.Text = btn.Text;
                }
            }
        }
        

        .NET

        <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication7.WebForm1" %>
        
        <!DOCTYPE html>
        
        <html xmlns="http://www.w3.org/1999/xhtml">
        <head runat="server">
            <title></title>
        </head>
        <body>
            <form id="form1" runat="server">
            <div>
        
                <asp:Panel ID="Panel1" runat="server">
                </asp:Panel>
                <asp:Button ID="Button1" runat="server" OnClick="Button1\_Click" Text=&q
        
        G Offline
        G Offline
        Gopi Kishan Mariyala
        wrote on last edited by
        #3

        When you check or uncheck the radio button, there is no code which does postback in your case. You have attached the event handler to the radio button but did not call the method.

        S 2 Replies Last reply
        0
        • G Gopi Kishan Mariyala

          When you check or uncheck the radio button, there is no code which does postback in your case. You have attached the event handler to the radio button but did not call the method.

          S Offline
          S Offline
          salar1369
          wrote on last edited by
          #4

          Thanks for the reply. may i know how to fix it? i am new to this programming language please guide me

          1 Reply Last reply
          0
          • G Gopi Kishan Mariyala

            When you check or uncheck the radio button, there is no code which does postback in your case. You have attached the event handler to the radio button but did not call the method.

            S Offline
            S Offline
            salar1369
            wrote on last edited by
            #5

            Thanks for the reply... may i know how can i fix the issue?im really new to this world of language

            1 Reply Last reply
            0
            • Kornfeld Eliyahu PeterK Kornfeld Eliyahu Peter

              As you radio buttons are dynamic there will not be exist on the next post-back. You have to recreate them every time you got a post-back, so you have to maintain some state that tells you if there was radio buttons. As now the only time you create the radio button is on Button.OnClick...

              I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

              S Offline
              S Offline
              salar1369
              wrote on last edited by
              #6

              thanks for the reply... i dont understand i tried to save the amount of buttons in ViewState["Counter"] , is that what i must do?

              Kornfeld Eliyahu PeterK 1 Reply Last reply
              0
              • S salar1369

                thanks for the reply... i dont understand i tried to save the amount of buttons in ViewState["Counter"] , is that what i must do?

                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu PeterK Offline
                Kornfeld Eliyahu Peter
                wrote on last edited by
                #7

                That not enough, you also have to recreate them outside the click event - which does not happening on other post-back events, like click on one of the radio buttons... For instance you can add a loop of creation to page's OnLoad when it's post-back and the source isn't the button...

                I'm not questioning your powers of observation; I'm merely remarking upon the paradox of asking a masked man who he is. (V)

                "It never ceases to amaze me that a spacecraft launched in 1977 can be fixed remotely from Earth." ― Brian Cox

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups