Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

disable a radio button group 1

Status
Not open for further replies.

aspnet98

MIS
May 19, 2005
165
0
0
US
I am using the following code below along with a simple loop to generate radio button groups. They are dynamic with what is in a sql database table in a bit field. They get values of 1 and 0 when select and submited. I have a field called status in the record set that is used to created these radio buttons during the loop. It is either Lock or UnLock. When it Lock I need to disable the radio buttons so people cannot select them but they can see what was select prior to the locking.

Is this possible? Please help...

Radio Button Group:

<td nowrap><input name="txtNum<%= intRecID %>" type="Radio" onClick="RecUpdate('<%= intRecID %>')" value="1" size="20" <%If (CStr((Recordset1.Fields.Item("RecNum").Value)) = CStr("1")) Then Response.Write("checked") : Response.Write("")%>>

<td nowrap><input name="txtNum<%= intRecID %>" type="Radio" id="txtNum<%= intRecID %>" onClick="RecUpdate('<%= intRecID %>')" value="0" size="20" <%If (CStr(Abs((Recordset1.Fields.Item("RecNum").Value))) = CStr("0")) Then Response.Write("checked") : Response.Write("")%>>
 
Code:
<%
if CStr((Recordset1.Fields.Item("lock").Value) = "True" then
   radio_disabled = "disabled"
else
   radio_disabled = ""
end if
%>

<input type="radio" <%=radio_disabled%> ....>

This will only disabled if the Lock variable is true.
 
Where do I place this and will it work when it loops?

Do I have to reference the name?
input name="txtNum<%= intRecID %>" ????
 
Sure you can put it in a loop.

On thing that helps me when I'm stuck on something like this is to try to make a plain static HTML page that displays like I want. This helps reduce confusion between the ASP script logic errors and places where the script is doing exactly what you want but the HTML output still isn't pleasing.
 
How do I work the code into what I posted?

<td nowrap><input name="txtNum<%= intRecID %>" type="Radio" onClick="RecUpdate('<%= intRecID %>')" value="1" size="20" <%If (CStr((Recordset1.Fields.Item("RecNum").Value)) = CStr("1")) Then Response.Write("checked") : Response.Write("")%>>

<td nowrap><input name="txtNum<%= intRecID %>" type="Radio" id="txtNum<%= intRecID %>" onClick="RecUpdate('<%= intRecID %>')" value="0" size="20" <%If (CStr(Abs((Recordset1.Fields.Item("RecNum").Value))) = CStr("0")) Then Response.Write("checked") : Response.Write("")%>>


I tried placing your code below this and it did not work. Can we work the if statement into my if statements as a nested if somehow?

Please elaborate, I am new to this ASP.
 
I did it this way but I don't think it is efficient. I think it works though....

<%
if CStr((Recordset1.Fields.Item("lock").Value)) = "Locked" then
radio_disabled = "disabled="&"true"
else

end if
%>

<td nowrap><input name="txtNum<%= intRecID %>" type="radio" <%=radio_disabled%> onclick="RecUpdate('<%= intRecID %>')" value="1" <%If (CStr((Recordset1.Fields.Item("RecNum").Value)) = CStr("1")) Then Response.Write("checked") : Response.Write("")%>>
 
My way failed. If one record was locked and the rest unlocked, all the radiobuttons are disabled...

It need only disable the button that has a lock status...

 
You have two options: reset to radio_disabled to zero length string "" just before the if statement
Code:
<%
radio_disabled = ""
if CStr((Recordset1.Fields.Item("lock").Value)) = "Locked" then
   radio_disabled = "disabled=true"
end if
%>

or write the whole HTML tag inside the if statement:

Code:
<%
if CStr((Recordset1.Fields.Item("lock").Value)) = "Locked" then %>
  <input type=radio disabled=true ...>
<%
else %>
   <input type=radio ...>
<% end if %>

You are either the slave of what made men or what men made.
 
Great Thanks so much.

One last request. I need to make sure the user makes a selection before they click submit. I want to use javascript for this and not check the database. The code above is how I generate the radio buttons dynamically. Can you show me how to add a javascript (on submit validator) for the dynamic code I have that loops and creates radio buttons. I just want to say, "You missed a selection and keep the focus on that blank radio buttton.

I had posted this in javascript weeks ago and really got know help on it.

Best Regards!
 
You want a function called by the HTML form's onSubmit event. The function will return true or false. If it returns false, the form is not submitted.

Inside the function, you will check for the criteria that the form data must meet before it is OK to submit. If the data falls below the criteria, you will display an alert messagebox to inform the user of the problem, and then return false. If the data meets all of your criteria, the function simply returns true.

So what is the criteria? If your ASP code sets one of the radio buttons in each pair to checked then you won't simply be able to determine if any pair exists where both are unchecked, because your ASP code ensures that one of them will always be checked. And what about the ones that are locked? Unless I misunderstood the previous postings in this thread, the user wont even be able to select the disabled radio buttons.

So the steps you need are:
1. Determine form submittal criteria.
2. Write a function that uses the criteria to return true or false.
3. Make sure the function displays an alert when a problem is found.
4. Call the function in the form's onSubmit event.
5. Try here: forum216 for detailed questions about client-side scripting.
 
Great this is a start. Sheco do appreciate your help on all accounts as I am very new to ths sytle coding...

Can you help me with the code?
1) The database field is a bit field. 1 = the left radio button 0 = the right radio button.

I need to check when the person submits if each and every radio button has a 1 or a zero, if it does not NO SUBMIT and focus on the first button not selected?

Can you help me with that. It is far to complex for me to code at this stage. I can provide all I have written on the page so far, loop and radio button setup (Like I posted above).

Thanks so much and a start to you.

Cheers
 
Please clarify this:
aspnet98 said:
... every radio button has a 1 or a zero ...

From my understanding of the discussion above, your ASP code already creates each radio buttons with either a 1 or 0... and of each pair, one or the other will be "checked" unless the bit field is null.

I'm afraid I still don't understand the criteria.
 
First and foremost the radio buttons are null. I made a mistake in explaining. The field is not a bit field it is a int field type. Initially it is set to 3 for every record that produces the radio button for. When a user logs in they select from the left 1 or right 0. The db always has a value set, 3 being the initial value. I need to check for 3 and if so prompt users to make a selection.

Sorry and can this be done?
 
Ah, I think I understand, but let me try to put it in my own words to make sure:

A. You have a table with a field named RecNum.
B. For each row in the table, the value of RecNum will be 1, 2, or 3.
C. Your ASP loops through rows of a recordset derived from this table.
D. The loop produces a pair of radio buttons for each row.
E. If a row's value of RecNum is 1 or 2, the corresponding radio button is checked.
F. Conversely, if a row's value of RecNum is 3, neither radio button is checked.


Now, assuming that my understanding is correct, you need a function that alerts the user and returns False if any pair of radio buttons has both buttons unchecked, but otherwise it returns True.

In order to do this you need code that will be able to:
A. Recognize which radio buttons are paired with which.
B. Distinguish members of a pair from each other.

Part A is easy because all radio buttons in the same group have the same value in the name property, thats what makes them in the same group.

Part B is also easy, just make sure every button has a unique but predictable value in its id property.

In the very first post I notice that only the 2nd radio button has a value in its id property... the other one will also need an id but its value must be different. My preference would be to have values like this:
id="ROne<%= intRecID %>"
- and -
id="RZero<%= intRecID %>"

... but thats not a strong preference... give them whatever values you like as long as each record will end up with a value that is unique on the page.

I think I should wait for you to confirm that I am understanding your criteria before I go off and write a function that might not be what you want.



 
You are awesome sheco!

You are right on the money.

I do have a unique ID for the radio buttons' record

I store the intRecID here:
<% intRecID =(Recordset1.Fields.Item("RecID").Value) ' Store the current RecordID in a variable %>

The two buttons have the same name because they are a group:
<input name="txtNum<%= intRecID %>"
<input name="txtNum<%= intRecID %>"

Does this allow you to proceed?

 
Alright I made you a little example page that is plain HTML so you can just cut and paste the code below into a new text document on your desktop and then change the file extension from .txt to .htm

... then you can open it in the browser by just clicking on the file... or drag-n-drop into a browser window, whatever...
Code:
<html>
 <head>
  <title>
   OnSubmit Example Page
  </title>
  <script>
   function SubmitOK()
   {
     if (!document.Demo.ROne1.checked && !document.Demo.RZero1.checked)
     {
       alert("Please chose an answer for Item #1");
       return false;
     }
     if (!document.Demo.ROne2.checked && !document.Demo.RZero2.checked)
     {
       alert("Please chose an answer for Item #2");
       return false;
     }
     if (!document.Demo.ROne3.checked && !document.Demo.RZero3.checked)
     {
       alert("Please chose an answer for Item #3");
       return false;
     }
     if (!document.Demo.ROne4.checked && !document.Demo.RZero4.checked)
     {
       alert("Please chose an answer for Item #4");
       return false;
     }
     if (!document.Demo.ROne5.checked && !document.Demo.RZero5.checked)
     {
       alert("Please chose an answer for Item #5");
       return false;
     }

     // if we made it this far in the code, return true
     return true;
   }
  </script>
 </head>
 <body>
  <form name="Demo" OnSubmit="return SubmitOK();" action="" 

method="POST">
   Item #1
   <input name="txtNum1" type="Radio" id="ROne1" value="1">
   Red
   <input name="txtNum1" type="Radio" id="RZero1" value="0">
   Blue
   <hr>
   Item #2
   <input name="txtNum2" type="Radio" id="ROne2" value="1">
   Coffee
   <input name="txtNum2" type="Radio" id="RZero2" value="0">
   Tea
   <hr>
   Item #3
   <input name="txtNum3" type="Radio" id="ROne3" value="1">
   Yes
   <input name="txtNum3" type="Radio" id="RZero3" value="0">
   No
   <hr>
   Item #4
   <input name="txtNum4" type="Radio" id="ROne4" value="1">
   Tall
   <input name="txtNum4" type="Radio" id="RZero4" value="0">
   Short
   <hr>
   Item #5
   <input name="txtNum5" type="Radio" id="ROne5" value="1">
   Dog
   <input name="txtNum5" type="Radio" id="RZero5" value="0">
   Cat
   <br><br>
   <input type="submit">
  </form>
 </body>
</html>

I did this in plain HTML & JavaScript so we could concentrate on the validation function and not worry about the ASP code. Notice inside the SubmitOK function that we reference the radio buttons not by their name, but rather by their id. The reason for this is that id is unique per radio button.

Back to the ASP, you can use a loop to write the javascript function the same way you use it to write the radio buttons. Another way would be to use a hidden form input element to hold the total number of raido button pairs, and then use the value of that element in a fixed javascript loop.
 
I do not think that method will work. I have over 11,000 transaction ids in my database and that id is the one I use:
<% intRecID =(Recordset1.Fields.Item("RecID").Value) ' Store the current RecordID in a variable %>


How can I write the loop instead of manually coding item 1 2 3 etc.

I really am not following this all and need some more indepth help...
 
It can definately be done in a loop.

The purpose of that code above is to show you an example of calling a validation function that determines if it is OK to submit an HTML form based on the radio button criteria.

Play around with that code and see if it accurately models the validation behavior that you require. If it does, we can move forward with the looping, if not then we should get our logic straight before adding another level of difficulty.

 
The only thing that is not right is
id="ROne"
id="RZero"

my ids are coded with the same name...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top