on selection of Radio button

Rani

Well-known member
Joined
Nov 19, 2005
Messages
71
Programming Experience
Beginner
I have two radio buttons yes/no on my web page showing yes or no based on the values in the database field 'registration sent' boolean type 0 or 1.

If the user wants to change this value on the page, i want to send a confirm box "are you sure you want to make changes?".

Originally, i had an input type submit button to change the values after selecting the radio button.
After research i was able to get this done.

but, the requirement from my boss is to show the confirm box just after the selection of a radio button with out the input submit button in addition. he does not want any additional button. just the radio buttons.

my code was on the page load i had
1.
me.btnchangeregistration.attributes.add("onclick","return confirm('are you sure?');")

and
2.
in html i had a input type submit button.
and
3.
private sub btnchangeregistration_serverclick(byval sender as system.object, byval e as system.eventargs)handles btnchangeregistration.serverclick
if me.rdoReg.checked then
sql to update the field to 1
else
sql to update the field to 0
end if
end sub

The above actually works. shows the confirmbox when user selects one of the radio's and click the button to change the values, confirm box appears and based on the user decision ok/cancel changes the database field.

*Now this requirment is changed.

I want to be able to do this with out the button. How can i do this with just the selection of the radio button to show the confirm box and based on the ok/cancel be able to update the field in the database?

help me out with this or direct me to an online article were i can see a sample.

Thanks
 
What you ask for can be done, it requires a combination of client-side Javascript and server-side code, and for the latter you must set the AutoPostBack property on the RadioButtons to True, it is False by default for these controls. Then you add 'onmousedown' attributes where you also pass the object reference. In Javascript handler this intervenes the real click event and you take full control in the client script from here. First you check in radiobutton is already checked, which means there is no change. Else you use the confirm functionality to ask user if the change was intentional or to tell about consequences etc. If user confirms you manually change the checked value. Finally you must invoke Postback when there was a change to pass control of the series of events to server which does database update immediately (as you asked), this is done by invoking the click event of the radiobutton. In server-side code you handle the CheckedChanged event of each RadioButton and do database code there.
Here is example of server code:
VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
  RadioButton1.Attributes.Add("onmousedown", "confirmit(" & RadioButton1.ClientID & ");")
  RadioButton2.Attributes.Add("onmousedown", "confirmit(" & RadioButton2.ClientID & ");")
End Sub
 
Protected Sub RadioButtons_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged
  Me.Title = sender.ID & " checked value was changed to " & sender.Checked
  'here you do that database stuff you mentioned, but just test out the functionality first
End Sub
Here is example of javascript:
HTML:
<script type="text/javascript">
function confirmit(obj) {
if (obj.checked==false){
if (confirm("confirm change")==true) {
obj.checked=true;
obj.click();
}
}
}
</script>
 
Last edited:
Thank you very much for your time and your response.

Since i am new understanding is a bit slow.

I have few clarifications about your response.
1. If i had my radio buttons named rdoyes and rdonow
would it be rdoyes.clientid? if not what is client id?
RadioButton1.ClientID

2. Me.Title = sender.ID & " checked value was changed to " & sender.Checked
- I am not clear on this if you could be more specific and write comments to understand that would be great.

3. what does the javascript here does? can you comment please?

function confirmit(obj) {
if (obj.checked==false){
if (confirm("confirm change")==true) {
obj.checked=true;
obj.click();}}}

Thank you very much again.
 
1. RadioButton1 is the ID yes. ClientId is usually the same, but in some cases ASP.Net need to change the ID for it to be unique, which it has to be.

2. just some visual guidance for you to see how it works, and also help of the same to see what 'sender' is. try it out and you will see.

3. what the javascript does is thoroughly described in the post, read the post again. try it out and you will see.
 
Is this an input type radio or an asp:radio.
can you write to me what would be your html? i would get a better idea to solve this. thanks
 
when i try this, i get an error
Me.rdoYes.Attributes.Add("onmousedown", "return confirmit(" & RadioButton.rdoYes & ");")
rdoyes is not a member of system web ui webcontrols.radiobutton.
If i saw your radiobutton in html i would get a better idea.
Thanks
 
It is a regular asp radiobutton, I also told you that you had to set the AutoPostBack property to True, which implies that this could not just be a html input tag.
If you have set the id of your radiobutton1 to rdoYes, then you must change the radiobutton1.clientid to rdoyes.clientid.
my html:
HTML:
<asp:RadioButton ID="RadioButton1" runat="server" GroupName="gr1" AutoPostBack="True" />
<asp:RadioButton ID="RadioButton2" runat="server" Checked="True" GroupName="gr1" AutoPostBack="True" />
 
Back
Top