views:

1156

answers:

4

Hi! I`m trying to add another eventHandler to RadioButton. This is the sample code(which is working):

asp.net:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:Button ID="Button1" runat="server" Text="Button" />

C#:

protected void Page_Load(object sender, EventArgs e)
{
    RadioButton RB1 = new RadioButton();
    RB1.ID = "1";
    RB1.GroupName = "bla";
    RB1.CheckedChanged += new EventHandler(CheckedChanged);
    RadioButton RB2 = new RadioButton();
    RB2.ID = "2";
    RB2.GroupName = "bla";
    RB2.CheckedChanged += new EventHandler(CheckedChanged);
    PlaceHolder1.Controls.Add(RB1);
    PlaceHolder1.Controls.Add(RB2);

}
protected void CheckedChanged(object sender, EventArgs e)
{
    Label1.Text = ((RadioButton)sender).ID;
}

In my project I have dynamic creating of RadioButtons (the number of rows I get from database). The same adding eventHandler does not work, but if I write

MyRadioButton.Load += new EventHandler(Another_method);

The Another_method will start, but in

MyRadioButton.CheckedChanged += new EventHandler(Main_method);

the Main_method will not start if I choose one of the RadioButton`s. What is wrong?

A: 

When you say the Main_method will not start, do you mean when you click the radio button nothing happens (I.e. No Postback)?

If you want this behavior where a radio button is clicked/checked and the event fires, you'll need to also do this for each dynamic radiobutton:

MyRadioButton.AutoPostBack = true;

Then in your handler (assuming you're using one handler for all of the dynamic radio buttons), you can get the button by:

protected void MyRadioButton_CheckedChanged(object sender, EventArgs e)
{
   RadioButton selectedButton = (RadioButton)sender;

   //do whatever you need with the button using selectedButton variable
}

If you aren't looking for the AutoPostBack functionality, can you elaborate on what else is going on, on the page? How is the postback invoked? (I.e. Another button? etc.)

KP
+1  A: 

If I am not mistaken, with dynamic controls in ASP.Net, you need to "rewire" their events on a postback. Remember that on a postback, dynamic controls are no longer there. You have to recreate them.

Eclipsed4utoo
A: 

@KevinP This is my code:

    Table tb1 = new Table();
    PlaceHolder1.Controls.Add(tb1);
    TableRow tr = new TableRow();
    TableCell tc = new TableCell();
 //adding the first row with title"
    tr.Cells.Add(tc);
    for (int i = 0; i < ((ArrayList)(result[0])).Count; i++)
    {
        tc = new TableCell();
        Label example = new Label();
        example.Text = ((columnsResultFromSqlClients)(i)).ToString();
        tc.Controls.Add(example);
        tr.Cells.Add(tc);
    }
    tb1.Rows.Add(tr);
    //filling the table
    for (int i = 0; i < result.Count; i++)
    {
        tr = new TableRow();
        tc = new TableCell();
  //adding radio button
        RadioButton RB = new RadioButton();
        RB.Attributes.Add("value", ((ArrayList)(result[i]))[0].ToString());
        RB.GroupName = "for_selecting";
        RB.ID = ((ArrayList)(result[i]))[0].ToString();
        RB.CheckedChanged += new EventHandler(RB_CheckedChanged2);
        //RB.AutoPostBack = true;

        RB.Attributes.Add("AutoPostBack", "True");
        tc.Controls.Add(RB);
        tr.Cells.Add(tc);
  //adding content
        for (int j = 0; j < ((ArrayList)(result[i])).Count; j++)
        {
            tc = new TableCell();
            Label example = new Label();
            example.Text = ((ArrayList)(result[i]))[j].ToString();
            tc.Controls.Add(example);
            tr.Cells.Add(tc);
        }
        tb1.Rows.Add(tr);
    }

If I use RB.AutoPostBack = true;, I have no time to press the button to submit my choice, cause the page will reload when i click the one of the Radio Buttons. Also the RB_CheckedChanged2 code:

protected void RB_CheckedChanged2(object sender, EventArgs e)
{
    RadioButton tempRB = (RadioButton)sender;
    if (tempRB.Checked)
    {
        selected_id = tempRB.ID;
    }
}

The select_id is a static int varible with standart value = "-1".

WISEMAN
A: 

I want to handle using javascript event handler. Appreciate your help.

prashkam