Dynamic created radio buttons wont execute event on click (c# .net )
-
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
-
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
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)
-
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
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.
-
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.
-
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.
-
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)
-
thanks for the reply... i dont understand i tried to save the amount of buttons in ViewState["Counter"] , is that what i must do?
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)