Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Javascript set button enable on checkbox true
Message
De
16/10/2010 11:08:35
Timothy Bryan
Sharpline Consultants
Conroe, Texas, États-Unis
 
 
À
15/10/2010 13:00:12
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Versions des environnements
Environment:
C# 4.0
Application:
Web
Divers
Thread ID:
01485567
Message ID:
01485728
Vues:
37
>>>>>
>>>>>Sorry, I'd forgotten about that behaviour. I guess, if the checkbox and button are at the same level then this would work in the function:
var s = checkbox.id.substr(0, checkbox.id.lastIndexOf("_") + 1) + "btnAgreeContinue"; ;
>>>>>var button = document.getElementById(s);
>>>>>button.disabled = !checkbox.checked;
but there must be something a bit more elegant.....
>>>>
>>>>
>>>>This didn't work either.
>>>
>>>Worked for me with a usercontrol in a form but it does depend on both elements being at the same level. Given that, I don't know why it didn't work for you. But it is ugly anyway.....
>>
>>My two controls are at the same level.
>>
>>>
>>>> I tried my other code to FindControl and pass that in such as this so I could see what the button id was and surprising it is 'btnContinue".
>>>
>>>So, given that the compiler didn't dream that up from scratch where did the name stem from ?
>>>
>>>> So very frustrating to get such a simple thing to work. These things make me want to stick to WPF or Silverlight and avoid ASP.net.
>>>
>>>Very true. Would just have been a simple binding in WPF/Silverlight....
>>>
>>>>
>>>>mmButton thisButton = (mmButton)this.FindControlByName(this, "btnAgreeContinue");
>>>>
>>>>StringBuilder sb = new StringBuilder();
>>>>sb.Append(@"<script language='javascript' type='text/javascript'>");
>>>>sb.Append(@"function SetAgreeAndContinue(checkbox) {");
>>>>sb.Append(@"var button = document.getElementById('" + thisButton.ID + "');");
>>>>sb.Append(@"button.disabled = !checkbox.checked;");
>>>>sb.Append(@"}");
>>>>sb.Append(@"</script>");
>>>>
>>>>string myscript = sb.ToString();
>>>>Page.RegisterStartupScript("S", myscript);
>>>>
>>>
>>>So what does FindControlByName() look like ?
>>
>>That was a method someone posted for me here in VB.net and I converted. It works pretty well and does return this control just fine. Here it is again.
>>
>>public Control FindControlByName<T>(T controlToSearch, string controlID)
>>			where T : System.Web.UI.Control
>>{
>>     Control matchingControl = null;
>>
>>    try
>>    {
>>        foreach (Control subControl in controlToSearch.Controls)
>>        {
>>            if (subControl.ID != null && subControl.ID.EndsWith(controlID))
>>            {
>>	matchingControl = subControl;
>>	break;
>>            }
>>            else
>>            {	
>>	if (matchingControl != null)
>>	    // found it
>>	    break;
>>                     else if (subControl.HasControls())
>>                    {
>>	    matchingControl = this.FindControlByName(subControl, controlID);
>>                    }
>>            }
>>        }
>>    }
>>    catch (Exception e)
>>    {
>>        throw;
>>     }
>>    return matchingControl;
>>}
>>
>
>Thanks for the code.
>But there's no guarantee that the original Id is not duplicated elswhere on the page. So it wouldn't, for example, differentiate between
>'ControlA_Control_B_btnAgreeContinue' and 'ControlA_ControlC_btnAgreeContinue' so you couldn't use the same user control twice on the same form.
>My version would avoid that problem - or it would if it worked :-{
>Did you debug to see where it was going wrong for you ?


I am going to experiment some more with getting the Id using your version. I did get it to work with this, but hard coding the ID is not what I want or need.
protected override void OnInit(EventArgs e)
{
    StringBuilder sb = new StringBuilder();
    sb.Append(@"function SetAgreeAndContinue(checkbox) {");
    sb.Append(@"var button = document.getElementById('ctl00_ctl00_Retirement Mangement System_cphOuterContent_cphMainContent_RightContent_ctrlAgreement_btnAgreeContinue');");
    sb.Append(@"button.disabled = !checkbox.checked;");
    sb.Append(@"}");

    Page.ClientScript.RegisterClientScriptBlock(GetType(), "AgreementScript", sb.ToString(), true);
    base.OnInit(e);
}
At least I know that is the ClientID of the control at runtime, so I will get the clientId of the checkbox to see if it is a match minus the ending. I fear the spaces may be an issue also, so I may track down how this is calculated and make some nameing changes. Like RMS instead of that long name whereever that comes from.
Tim
Timothy Bryan
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform