473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Event Fires twice for dynamic RadioButtonList

I created a page that reads a DB for questions and possible answers
(usuallyYes/No). I create a panel for each group of questions, and add a
panel for each question to the Group panel. To the Question panel I add a
label with the question text, and a radiobuttonlist with the answers. I
have an eventhandler I add to each radiobuttonlist, which is the same for
all.

The Group panels are then added to Placeholder1.Controls. I then add each
panel to an arraylist arlPanels and do a "Session["arlPanels"] = arlPanels;"
when all are created. This is done in CreatePanesl(). In PersistPanels(),
I get the arlPanels from Session, and add them to Placeholder1.Controls.
This all works pretty good so far. When I click an answer to question 1,
the event fires, no problem. However, when I then click an answer to
question 2, the event fires for question 1 again, and then question 2. The
only way I can seem to prevent this is to set the radiobuttonlist Enabled =
false; in the eventhandler..

I am kinda new to dynamically creating controls in ASP.NET, but I have read
that there are glitches. I would appreciate any suggestions.

In Page_Load:
IF (!IsPostBack)
{
CreatePanels();
}
else
{
PersistPanels();
}

--
Mike
Nov 19 '05 #1
5 3347
This is normal. You are changing both radio button list values (and
you are using SelectedIndexChanged), so when you do a postback both
events are fired. Probably if you leave question 1 alone that won't
fire.

I'd use the radiobuttonlist(s).SelectedValue(s) instead in the submit
button click postback hander instead to determine what the values are.

BTW, why are you using Session? Why not just:
In Page_Load:
CreatePanels();

Nov 19 '05 #2
Actually, when I click an answer to question #1, it immediately posts back.
I need to do this as the visibility of subsequent questions depends on
previous answers. Then when I click an answer to question #2, the
eventhandler fires for question #1 again, AND then for question #2
immediately after that. Weird!

I use Session, as there are a lot of questions/answers, and I create then
from classes loaded from a DB at Application Start. Retrieving the panels
from Session is quicker:

private void PersistPanels()
{
PlaceHolder1.Controls.Clear();
arlPanels = Session["arlPanels"] as ArrayList;
if (arlPanels!=null)
{
Panel pnl;
for (int i = 0;i < arlPanels.Count;i++)
{
pnl = (Panel) arlPanels[i];
PlaceHolder1.Controls.Add(pnl);
}
}
}

<sa*************@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
This is normal. You are changing both radio button list values (and
you are using SelectedIndexChanged), so when you do a postback both
events are fired. Probably if you leave question 1 alone that won't
fire.

I'd use the radiobuttonlist(s).SelectedValue(s) instead in the submit
button click postback hander instead to determine what the values are.

BTW, why are you using Session? Why not just:
In Page_Load:
CreatePanels();

Nov 19 '05 #3
It isn't weird. Both radio button lists are have changed values from
the time they were built. When you click one you cause the whole
pageto be rebuilt, and that's why the change value event fires for
both. Thats why I think if you put the question 1 answer back to its
default value and then click question 2 you will only get the question
2 event. You might try this just for experimentation.

I forgot that you have AutoPostBack set to true for these controls so
when you click them a postback happens automatically. I understand
that you just want the new question event to fire. How to solve?
Hmmmmm... Can you use different event handlers? You could also detect
the GroupName property of the individual items in the RadioButtonList
to see which question has been clicked. I know you must be giving them
unique values on the form or they wouldn't function as grouped radio
buttons. So your event handler would look like
if(radioButton.item[0].GroupName == "question1") {... do stuff }

Anyway, if anyone else has ideas?

Nov 19 '05 #4
My current solution is shown below. I setup a Hashtable which stores the ID
of the radiobutton list (which contains the Group# and Question #) and the
SelectedValue. When the event fire for a radiobutton list, it checks to see
if that ID/Value combo exists in the Hashtable. If the exact ID/Value
combination exists, it returns from the event handler. Otherwise it
adds/replaces the ID/Value.

The one thing noticed is that the radiobuttonlists retain their values
between postbacks, which is what I want. I do not want then to revert back
to their original values. I do, however want the user to be able to go back
and change them, which is going to make the conditional display of
subsequent questions interesting. (If you go back and kill your
grandfather, you no longer exist!) This is a user requirement, so that is
what I need to do.

Anyway, thanks for your help. Any more creative solutions would be
appreciated.
//----------------------------------------------------------------
// Generic Event for Question RadioButtonList
//----------------------------------------------------------------
private void rblQuestion_SelectedIndexChanged(object sender,
System.EventArgs e)
{
htQuestionAnswer = Session["htQuestionAnswer"] as Hashtable;
RadioButtonList rbl;
rbl = (RadioButtonList) sender;

if (htQuestionAnswer.ContainsKey(rbl.ID))
{
if (htQuestionAnswer[rbl.ID].ToString() == rbl.SelectedValue)
{
// Ignore dup event
return;
}
else
{
// replace value, save Hashtable
htQuestionAnswer[rbl.ID] = rbl.SelectedValue;
Session["htQuestionAnswer"] = htQuestionAnswer;
}
}
else
{
// add value, save Hashtable
htQuestionAnswer.Add(rbl.ID,rbl.SelectedValue);
Session["htQuestionAnswer"] = htQuestionAnswer;
}

// Logic goes here
}

<sa*************@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
It isn't weird. Both radio button lists are have changed values from
the time they were built. When you click one you cause the whole
pageto be rebuilt, and that's why the change value event fires for
both. Thats why I think if you put the question 1 answer back to its
default value and then click question 2 you will only get the question
2 event. You might try this just for experimentation.

I forgot that you have AutoPostBack set to true for these controls so
when you click them a postback happens automatically. I understand
that you just want the new question event to fire. How to solve?
Hmmmmm... Can you use different event handlers? You could also detect
the GroupName property of the individual items in the RadioButtonList
to see which question has been clicked. I know you must be giving them
unique values on the form or they wouldn't function as grouped radio
buttons. So your event handler would look like
if(radioButton.item[0].GroupName == "question1") {... do stuff }

Anyway, if anyone else has ideas?

Nov 19 '05 #5
sam
It looks good. Although you could probably replace it with this:

private void rblQuestion_SelectedIndexChang*ed(object sender,
System.EventArgs e)
{
htQuestionAnswer = Session["htQuestionAnswer"] as Hashtable;
RadioButtonList rbl;
rbl = (RadioButtonList) sender;
htQuestionAnswer[rbl.ID] = rb*l.SelectedValue;
Session["htQuestionAnswer"] = htQuestionAnswer;

// Logic goes here
}

cause I think hashtables will automatically add the key if it doesn't
exist and the rest of your code is just optimization.

Anyway, I may try to think of something else. Technically, I *am* at
work so work asp.net has to come first ;).

-Sam

Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Michael Johnson Jr. | last post by:
I am trying to handle a button click event, which updates a web control table with data. The button is dynamically created in the table itself. When I call updateTable() in the Page_Load the new...
7
by: Jay Douglas | last post by:
Hello all, I have a asp.net page that creates a pdf on the fly and sends the pdf down to the browser. When calling the page up in IE the Page_Load event is fried twice. This doesn't happen with...
4
by: Bill Manring | last post by:
I need to capture the event when the user closes the browser in my application. I have some code in the session_End event, which works fine when the session times out, but I need to end the...
1
by: Bill Manring | last post by:
The startup page for my ASP.NET application is an HTML frames page with two frames. This seems to cause the Session_Start event in the Global.asax file to fire twice. When I change the startup...
3
by: PKin via DotNetMonster.com | last post by:
Hi, I have a web page with a radioButtonList with 3 buttons (B1,B2 and B3) and a placeholder. B1 will put an .ascx file (Pl1.ascx) in the placeHolder, B2 will do the same with Pl2.ascx and B3...
9
by: Marcelo Cabrera | last post by:
Hi, I have a user control that in turn creates a bunch of webcontrols dynamically and handles the events these webcontrols raise. It used to work fine on ASP .Net 1.1 but when compiled on 2.0 it...
3
by: doctorle | last post by:
I'm surprised that the Current event of forms always fires twice (Access XP). I have quite a lot of processing done in the current event, how to make the code run just once? Thanks
5
by: docw | last post by:
SelectionChangeCommitted event fires twice Hi, Please have a look at the following ComboBox behavior. With the code below, if you click in the dropdown list with the mouse to select an item ,...
2
by: Simon Harvey | last post by:
Hi Guys, Can anyone tell me why the DataGridView.SelectionChanged event fires twice when I databind to it. If I do the following, the first row is selected automatically, but the changed...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.