Solved

Disable radio button list

Posted on 2006-11-05
29
354 Views
Last Modified: 2007-12-19
I have 2 radio lists, if somebody chooses 'Yes' for the first question then the 2nd question does not apply.
I thought it would be a nice touch to disable this 2nd control to give a visual clue but can't seem to get it to happen.
Here is the code, any idea why not working and/or how to make it happen?

  Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdNZresident.SelectedIndexChanged
        If RdNZresident.SelectedItem.Value = "Yes" Then
            RdNZpermit.Enabled = False
        End If
    End Sub
0
Comment
Question by:QPR
  • 13
  • 11
  • 3
  • +1
29 Comments
 
LVL 27

Expert Comment

by:Sammy
ID: 17878819
did you have the autopostback set to true for the first radio list?
0
 
LVL 12

Expert Comment

by:sandip132
ID: 17879135
then the 2nd question does not apply ???

What actually you want to do ?
0
 
LVL 29

Author Comment

by:QPR
ID: 17879251
I don't have any autopostback set to true but given that it is a server side control that sounds logical... can you expand please?
0
 
LVL 29

Author Comment

by:QPR
ID: 17879262
Sandip132....
It's pretty simple

Q1 yes or no.
Q2 if you said yes to Q1 then yes or no to question 2

If you said no to Q1 then disable/grey out the Q2 list.
Saves the user from given an illogical reply No to Q1 and Yes to Q2 makes no sense.
Greying out Q2 just gives a nicer user experience (to use that cliche)
0
 
LVL 5

Expert Comment

by:dynamicrevolutions
ID: 17879441
in your control, add autopostback=true
not adding that will not execute Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As....
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17879566
like dynamicrevolutions said if you dont set the autopostback to true the SelectedIndexChanged will never be reached
the other option is to use a clientside to disable and enable the radiobuttons based on the user's response.
the advantage of using a clientscript is saving app from making a round trip to the server.
http://www.houseofscripts.com/scripts/javascripts/disablerb.htm
if you decide on using a clientscript then all you need is to add an onclick event
this.myRadioButton.Attributes.Add("onclick",javascript:your_function_here();");

HTH
0
 
LVL 12

Expert Comment

by:sandip132
ID: 17879647
///Set  AutoPostback=True for RadioButtonList1

private void RadioButtonList1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if(RadioButtonList1.Items[0].Selected==true)
                  {
                        RadioButtonList2.Enabled=false;
                  }
                  else
                  {
                        RadioButtonList2.Enabled=true;
                  }
            }
0
 
LVL 29

Author Comment

by:QPR
ID: 17879673
thanks Dynamic - I'll give it a go when I'm back at work tomorrow.
Maybe the roundtrip overhead will counter act the small benefit of doing the disabling thing.
0
 
LVL 5

Expert Comment

by:dynamicrevolutions
ID: 17879684
the point should be given to sammy.
he mentioned about autopostback initially
0
 
LVL 29

Author Comment

by:QPR
ID: 17883626
autopostback didn't work unless I'm doing something wrong?
I highlighted the first control and set enablepostback to true in the properties.
Hit debug and tested - regardless of which option I choose in RList1 RList is still enabled
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17884542
Can you post your code so we can see why the postback is not responding??
0
 
LVL 29

Author Comment

by:QPR
ID: 17884662
Protected Sub RdNZresident_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdNZresident.SelectedIndexChanged
   If RdNZresident.SelectedItem.Value = "Yes" Then
      RdNZpermit.Enabled = False
   End If
End Sub


<asp:RadioButtonList ID="RdNZresident" runat="server" AutoPostBack="True">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>

<asp:RadioButtonList ID="RdNZpermit" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17891599
Your code runs fine
I havent changed a single line and it worked fine for me.
<asp:ListItem>Yes</asp:ListItem> ---> when click on this list item this
<asp:RadioButtonList ID="RdNZpermit" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:RadioButtonList>
will be disabled

if you want to toggle it then you should add an else statement
If RdNZresident.SelectedItem.Value = "Yes" Then
            RdNZpermit.Enabled = False
        Else
            RdNZpermit.Enabled = True
        End If

HTH
0
 
LVL 29

Author Comment

by:QPR
ID: 17892303
I have found the problem but have no idea how to fix it.
I'm working with a wizard control and the radio lists appear on page/section 4.
On page 5 I have a checkbox that must be ticked before the user can finish. (I have a client side javascript function to ensure this)
When I nav to page 4 and select the radio button RdNZresident I get a javascript error saying that document.form1.ChkAgree.checked is null or not an object. If I hit the "previous" button and then "next" to come back to the same page there is no error and RdNZpermit is diasbled!

So how come the postback is causing the JS function in default.aspx to error on postback but not when initially loaded?
The function is handled via <form id="form1" runat="server" onsubmit="return AgreeCheck()">
Perhaps the "next" button on the wizard is being treated as some kind of submit?
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 12

Expert Comment

by:sandip132
ID: 17905200
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17924251
>>Perhaps the "next" button on the wizard is being treated as some kind of submit?
wizard buttons are submit buttons
>>The function is handled via <form id="form1" runat="server" onsubmit="return AgreeCheck()">
whats the reason for this?
cant you use any other event instead of the form's submit?
0
 
LVL 29

Author Comment

by:QPR
ID: 17926104
"cant you use any other event instead of the form's submit?"
I'm open to suggestions.
I need to check to see if a checkbox has been checked prior to submitting the form (clicking "finish" on the wizards final step).
There is no validator for checkboxes and examples I've seen of making home made ones look long and complicated.
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17926705
>>There is no validator for checkboxes and examples I've seen of making home made ones look long and complicated.
would this be complicated?
<script type="text/javascript">
function ValCheck(source, args)
    {
    args.IsValid=document.getElementById("CheckBox1").checked;
    }
    </script>
<asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CustomValidator ID="CustomValidator1" runat="server" ErrorMessage="Check Box Not Checked" Display="dynamic" ClientValidationFunction="ValCheck"
            ></asp:CustomValidator>

I am using a CustomValidator here and if you want you can do more validation on the server as well.

HTH
Sammy
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17926708
0
 
LVL 29

Author Comment

by:QPR
ID: 17926755
>would this be complicated?
For a novice to .NET like myself yes... that said, I am a master of the copy and paste so thanks :)
So does this need an event to trigger it or do these server side customvalidators auto run?
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17927379
they are controlled by the framework and dont need the user interaction
post back if theres anything you dont understand about the custom validator.

0
 
LVL 29

Author Comment

by:QPR
ID: 17927584
Getting errors.
If I tick the checkbox and submit I get the error
If I don't tick the checkbox I get the validation text but still the error.
Error = Line 12, char 5 object expected.
I have double checked and it is called CheckBox1 in both the function and the form
This is the first block of code in the page including blank linkes, 12 seems to be the actual statement


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head><title>
      Vacancy Application
</title><link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
function ValCheck(source, args)
    {
    args.IsValid=document.getElementById("CheckBox1").checked;
    }
    </script>

</head>
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17927875
Object expected means the script can not see the the object "Control"
can you post your aspx code
0
 
LVL 29

Author Comment

by:QPR
ID: 17928048
Will post tomorrow when back at work, given it's a 5 page wizard control it's pretty long!
Are there any relevant bits you want to see or the whole thing?
0
 
LVL 27

Accepted Solution

by:
Sammy earned 250 total points
ID: 17928457
actually you dont need to post your code,
all you need is to run your page and view the source. you will see your checkbox1 id has changed to your wizarID_checkbox1

example
<asp:Wizard ID="Wizard1" runat="server" OnFinishButtonClick="Wizard1_FinishButtonClick">
            <WizardSteps>
                <asp:WizardStep runat="server" Title="Step 1">
                    <asp:CheckBox ID="CheckBox1" runat="server" />
                </asp:WizardStep>
                <asp:WizardStep runat="server" Title="Step 2">
                </asp:WizardStep>
            </WizardSteps>
        </asp:Wizard>
        <asp:Label ID="lbl" runat="server"></asp:Label>
if I do response.write here for the CheckBox1 clientID i will get Wizard1_CheckBox1 thats why you are getting object expected error
View the source of the page, see what the clientID is for checkBox and change it in your javascript

0
 
LVL 29

Author Comment

by:QPR
ID: 17933037
spot on - thanks
0
 
LVL 29

Author Comment

by:QPR
ID: 17933087
Meant to add..... when I run the site the output window has lots of warnings like this...
c:\websites\VApplications\Default.aspx.vb(56,0): warning BC42020: Variable declaration without an 'As' clause; type of Object assumed.

This line points to Dim strQuals6 = txtQuals6.Text
All the warnings point to similar dims.
Anything to worry about?
0
 
LVL 27

Expert Comment

by:Sammy
ID: 17934764
should be Dim strQuals6 as String=txtQuals6.Text
the warning is basically telling you are declaring variables without a specific type and the framework will assume all of these variables are meant to be objects.

correct the variable declaration to reflect the expected data they will hold.

HTH
0
 
LVL 29

Author Comment

by:QPR
ID: 17934811
Thanks, I'll do that.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now