Showing posts with label cascadingdropdown. Show all posts
Showing posts with label cascadingdropdown. Show all posts

Wednesday, March 21, 2012

Trying to make the CascadingDropDown read only

Hi, i'm trying to stop the CascadingDropDown value from being changed when the item the user is editing (via the formview control) is blocked. I notice there is no read only property on the DropDownList control or on the CascadingDropDown control so I figured the best way to do this was to disable the control and add a hidden field which would post the original value back.

<asp:Label ID="lblSectionIDValue" runat="server" Text='<%# Eval("SectionName") %>' Visible='<%# (bool)Eval("Blocked") %>'></asp:Label>
<asp:HiddenField ID="hfdSectionID" runat="server" Value='<%# Bind("SectionID") %>' Visible='<%# (bool)Eval("Blocked") %>' />
<asp:DropDownList ID="lstSectionID" runat="server" Visible='<%# !(bool)Eval("Blocked") %>'>
</asp:DropDownList>
<ajaxToolkit:CascadingDropDown ID="cddSectionID" runat="server" TargetControlID="lstSectionID" Category="Section" PromptText="-- Please Select --" LoadingText="[Loading sections...]" ServicePath="~/CascadingDropdownsService.asmx" ServiceMethod="GetSectionsList" SelectedValue='<%# Bind("SectionID") %>' Enabled='<%# !(bool)Eval("Blocked") %>' />

However a null value is being posted back instead of the original value. I've tried just about every combination now but nothing works. Without the CascadingDropDown control i can successfully say:

<asp:HiddenField ID="hfdSectionID" runat="server" Value='<%# Bind("SectionID") %>' Visible='<%# (bool)Eval("Blocked") %>' />
<asp:DropDownList ID="lstSectionID" runat="server" SelectedValue='<%# Bind("SectionID") %>' Enabled='<%# !(bool)Eval("Blocked") %>'>
</asp:DropDownList>

which posts the value back.

I think there is a bug in that some value is being sent back if the CascadingDropDown's enabled property is set to false and is overriding the value in the hidden field.

Hi here's a fix which creates a new property called EnabledOverride and works like the Enabled property for the standard controls such as the TextBox.

Edit CascadingDropDownExtender.cs:
============================

Add
-----
private const string stringEnabledOverride = "EnabledOverride";
-----

Add
-----
[DefaultValue("")]
[ExtenderControlProperty()]
public bool EnabledOverride
{
get
{
return GetPropertyValue(stringEnabledOverride, true);
}
set
{
SetPropertyValue(stringEnabledOverride, value);
}
}
-----

Edit CascadingDropDownBehaviour.js:
============================

Add
-----
this._enabledOverride = null;
-----

Add
-----
get_EnabledOverride : function() {
/// <value type="String">
/// Whether the drop down is enabled
/// </value>
return this._enabledOverride;
},
set_EnabledOverride : function(value) {
if (this._enabledOverride != value) {
this._enabledOverride = value;
this.raisePropertyChanged('EnabledOverride');
}
},
-----

Add (at the bottom of _setOptions function)
-----
_setOptions function (at the bottom):

// Disable the control if not enabled
if (!this._enabledOverride) {
e.disabled = true;
}
-----

Hope this helps you as much as it has me :).


You can try enclosing dropdown and cascadingdropdown in a server Div:





           <div runat="server" id="divEstado">



                                <asp:DropDownList ID="ddlEstado" runat="server" ></asp:DropDownList>



                                <cc1:CascadingDropDown ID="cddEstado" runat="server" TargetControlID="ddlEstado"



                                Category="Estado" PromptText="[Select value...]" ServicePath="Services.asmx"



                                ServiceMethod="FillData" UseContextKey="true" ParentControlID="ddlPais" /> 



          </div>



Disabling in code behind:



divEstado.Disabled = true;



Disabling in javascript:



$get('<%=divtrEstado.ClientID%>').disabled = 'disabled';


You can try enclosing dropdown and cascadingdropdown in a server Div:





           <div runat="server" id="divEstado">



                                <asp:DropDownList ID="ddlEstado" runat="server" ></asp:DropDownList>



                                <cc1:CascadingDropDown ID="cddEstado" runat="server" TargetControlID="ddlEstado"



                                Category="Estado" PromptText="[Select value...]" ServicePath="Services.asmx"



                                ServiceMethod="FillData" UseContextKey="true" ParentControlID="ddlPais" /> 



          </div>



Disabling in code behind:



divEstado.Disabled = true;



Disabling in javascript:



$get('<%=divtrEstado.ClientID%>').disabled = 'disabled';