开发者

Javascript for disabling dropdown in ASP.NET

开发者 https://www.devze.com 2022-12-26 01:36 出处:网络
I am using JScript in ASP.NET 2005. I have a page with a checkbox and a dropdown list. When the checkbox is checked, the dropdown is to be disabled. During Postback of the page, this behavior should b

I am using JScript in ASP.NET 2005. I have a page with a checkbox and a dropdown list. When the checkbox is checked, the dropdown is to be disabled. During Postback of the page, this behavior should be retained. I am using a javascript function as follows

if((chkOwner[1].checked==true))
{

   document.getElementById(ddlClientID).disabled=true;

   document.getElementById(ddlClientID).className = "form-disabled";

   document.getElementById(ddlClientID).selectedIndex = 0;
}

  else
 {

      document.getElementById(ddlClientID).disabled=false;开发者_运维技巧

      document.getElementById(ddlClientID).className = "form-input";

      document.getElementById(ddlClientID).selectedIndex = 0;
}

This works, almost. However, the dropdown selection is not retained after postback (when checkbox is not selected). [It goes to zero index's value]

Apprently the solution is to remove the last line, i.e, in the else part, remove the selectedIndex =0 .

But, when I do that the disabling of dropdown (when check box is checked) is not working after post back.

Could you please help me on this?

More Info: I am using ClientScript.RegisterStartupScript for checking this at each page load.

Thanks

Lijo

--CODE----- Following is what I am trying. (This is a sample application I created just now. It does not work. This can only show what I am trying to achieve.)

 function ManageInputsFor()
 {  
        if((document.getElementById(chbx).checked==true)) 
        {
            document.getElementById(ddlClientID).disabled=true;
            document.getElementById(ddlClientID).className = "form-disabled";
            document.getElementById(ddlClientID).selectedIndex = 0;
        }
        else 
        {
          document.getElementById(ddlClientID).disabled=false; 
          document.getElementById(ddlClientID).className = "form-input"; 
          document.getElementById(ddlClientID).selectedIndex = 0; 
        }
 }

 protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "chbx", @"<script language=""javascript"" type=""text/javascript"">var chbx ='" + CheckBox1.ClientID + "'</script>");
        ClientScript.RegisterClientScriptBlock(Page.GetType(), "ddl", @"<script language=""javascript"" type=""text/javascript"">var ddlClientID ='" + DropDownList1.ClientID + "'</script>");
        ClientScript.RegisterStartupScript(Page.GetType(), "onLoadCallForManageInputs", @"<script language=""javascript"" type=""text/javascript"">ManageInputsFor();</script>");

        CheckBox1.Attributes.Add("onclick", "ManageInputsFor();");

    }


you'll have to do a server side evaluation of the checkbox during the pastback, and set the dropdownlist enabled or not. the disabled attribute of the drop down isn't sent to the server during the post operation (neither is the value of the control when it's disabled,btw).

edit: to retain the value in the drop down across a postback, you can copy the selected value to another control which will be posted back, and then assign the value back to the drop down during server processing.


Basically, your problem is happening bc when you use javascript to change the state of an ASP control, that stat is not posted back to the server. You have to figure out a way to send it. Using a hidden control, or adding a parameter to the querystring are two such methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消