473,583 Members | 3,155 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Co ntrols. 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.Co ntrols.
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 3356
This is normal. You are changing both radio button list values (and
you are using SelectedIndexCh anged), 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).SelectedVal ue(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.Co ntrols.Clear();
arlPanels = Session["arlPanels"] as ArrayList;
if (arlPanels!=nul l)
{
Panel pnl;
for (int i = 0;i < arlPanels.Count ;i++)
{
pnl = (Panel) arlPanels[i];
PlaceHolder1.Co ntrols.Add(pnl) ;
}
}
}

<sa************ *@gmail.com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
This is normal. You are changing both radio button list values (and
you are using SelectedIndexCh anged), 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).SelectedVal ue(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 radiobuttonlist s 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_Sel ectedIndexChang ed(object sender,
System.EventArg s e)
{
htQuestionAnswe r = Session["htQuestionAnsw er"] as Hashtable;
RadioButtonList rbl;
rbl = (RadioButtonLis t) sender;

if (htQuestionAnsw er.ContainsKey( rbl.ID))
{
if (htQuestionAnsw er[rbl.ID].ToString() == rbl.SelectedVal ue)
{
// Ignore dup event
return;
}
else
{
// replace value, save Hashtable
htQuestionAnswe r[rbl.ID] = rbl.SelectedVal ue;
Session["htQuestionAnsw er"] = htQuestionAnswe r;
}
}
else
{
// add value, save Hashtable
htQuestionAnswe r.Add(rbl.ID,rb l.SelectedValue );
Session["htQuestionAnsw er"] = htQuestionAnswe r;
}

// Logic goes here
}

<sa************ *@gmail.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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_Sel ectedIndexChang *ed(object sender,
System.EventArg s e)
{
htQuestionAnswe r = Session["htQuestionAnsw er"] as Hashtable;
RadioButtonList rbl;
rbl = (RadioButtonLis t) sender;
htQuestionAnswe r[rbl.ID] = rb*l.SelectedVa lue;
Session["htQuestionAnsw er"] = htQuestionAnswe r;

// 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
2835
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 data from the button is not available at this time as to allow the table to properly update. So basically, I need to call UpdateTable() twice, once...
7
2095
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 mozilla, just IE. This is a big problem because the PDF can be 20+ MB in size and is causing some serious performance issues. Writing the PDF to...
4
4802
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 session immediately when the user closes the browser. Does anyone know a way of doing this? -- Thanks,
1
2646
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 page to an ordinary aspx page, the event only fires once. I would like to run some concurrent license checking code when the Session_Start event...
3
1807
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 with Pl3.ascx. Each .ascx file has controls that can fire som events(button, dropList...). When I start the page, B1 is default and every control on...
9
14432
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 does not. The problem is that the webcontrols get created on the OnLoad event of the usercontrol and the event handlers are assigned at the same...
3
5663
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
6800
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 , everything is fine. But if you use the Enter key to select in the dropdown list, the SelectionChangeCommitted event is fired twice.
2
9658
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 event fires twice: List<Trailertrailers = Trailer.SelectAll();
0
8328
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8195
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6581
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5375
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3820
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1434
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.