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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide cascading dropdown if no results returned 1

Status
Not open for further replies.

tomouse

Technical User
Aug 30, 2010
50
I have Ajax cascading dropdowns working well - I select a country and it returns the cities for that country, I select a city and it returns the hotels for that city. However, now I have a request that if no hotels are returned for the city, then the hotel dropdown (dplHotel) should be hidden and a text box (txtHotelManual) should be displayed instead.

The work for populating the dropdowns is done within a webservice - the webservice returns a list of CascadingDropDownNameValues. But none of this seems to involve the code behind. I somehow need to check whether any values have been returned and if not, I hide the dropdown and show the textbox. My code is here:
Code:
    <div class="divForm meiaColuna" style="float: left; clear: none;">
        <asp:Label ID="Label2" runat="server" Text="<%$ Resources:LocalizedText, city %>" />:
        <asp:DropDownList ID="dplHotelCidade" runat="server" Width="165" onChange="PersistControl(this,'hidIDCidade');ClearControl(this,'hidIDHotel');" />
        <asp:PlaceHolder ID="phCidade" runat="server" Visible="false">&nbsp;
            <img id="imbModalCidade" class="IconePlus" src="img/btn_mais.gif" onclick="void(abreDialog('AdminCidade')); scroll(0,0);" />
        </asp:PlaceHolder>
    </div>
    <div class="divForm" id="divHotel" style="clear: none;">
        *<asp:Label ID="Label3" runat="server" Text="<%$ Resources:LocalizedText, hotelname %>" />:
        <asp:DropDownList ID="dplHotel" runat="server" onChange="PersistControl(this,'hidIDHotel');" Width="220" />
        <asp:TextBox ID="txtHotelManual" runat="server" Visible="false" />
        <asp:PlaceHolder ID="phHotel" runat="server" Visible="false">&nbsp;
            <img id="imbModalHotel" class="IconePlus" src="img/btn_mais.gif" onclick="void(abreDialog('AdminHotel')); scroll(0,0);" />
        </asp:PlaceHolder>
    </div>

<asp:CascadingDropDown ID="ccddHotelPais" runat="server" Category="Pais" PromptText="<%$ Resources:LocalizedText, CCDDSelectCountry %>" ServiceMethod="GetPaises"
        ServicePath="~/wsDropDowns.asmx" TargetControlID="dplHotelPais" UseContextKey="true" />
    <asp:CascadingDropDown ID="ccddHotelCidade" runat="server" Category="Cidade" PromptText="<%$ Resources:LocalizedText, CCDDSelectCity %>" ServiceMethod="GetCidades"
        ServicePath="~/wsDropDowns.asmx" TargetControlID="dplHotelCidade" ParentControlID="dplHotelPais" />
    <asp:CascadingDropDown ID="ccddHotelHotel" runat="server" Category="Hotel" PromptText="<%$ Resources:LocalizedText, CCDDSelectHotel %>" ServiceMethod="GetHoteis"
        ServicePath="~/wsDropDowns.asmx" TargetControlID="dplHotel" ParentControlID="dplHotelCidade" />
I tried checking it with javascript but couldn't get it to work. Does anyone have any bright ideas/suggestions? Thanks,

Tom
 
Could you use javascript on the client?

Something like:

var v1 = document.forms[0].dplHotel(document.forms[0].dplHotel.selectedIndex).value;

if (v1 == "")
{
document.getElementById("dplHotel").style.display = "none";
}
 
Ah saw your post at the bottom of the page did you try registering the JS from the codebehind using like RegisterStartupScript("arrayScript1", scriptString); so the script runs at the bottom of the page?
 
Hey Wes, thanks for your reply. This is similar to the approach I was taking last night - I was running a javascript function after the user clicked the City dropdown to check if the Hotel dropdown was disabled.

However, it seems that the check of the Hotel dropdown occurs before the data from the webservice is returned, so I am not sure that this is th way.

I feel that I need some way of intervening *after* the webservice has returned its values to the Hotel dropdown - but I have no idea if that is possible.
 
Sorry, I replied before I read your second reply! :) Javascript is something I'm working at but I'm still learning. Would registering the script in the code behind address the issue in my last mail? Would this then run after the webservice had returned values to the hotel?
 
Yes :), if you use RegisterStartupScript it will put the javascript after the control has been loaded. Another nice thing you might want to use is you can pass the control.clientid from the codebehind into the script so the js wont have to find it might make it easier if your not used to js as much.
 
This sounds promising! But I have a question: when I have used RegisterStartupScript before, it has been to actually *trigger the call* of a clientside script from a code behind event.
However, the event in this case is the user clicking the City dropdown on the clientside (which triggers the webservice to populate the Hotel dropdown). I guess it does go serverside, because it is calling this webservice method to return the list of hotels from the SQL database, but because I am using the Ajax cascading dropdowns there isn't a full page postback.

So I guess I am asking: where should I put the RegisterStartupScript call? When I was trying to call my javascript function (CheckForHotels) I had it clientside on the City dropdown like this:
Code:
<asp:DropDownList ID="dplCity" runat="server" onChange="CheckForHotels(this);"/>
but this was checking the Hotel dropdown before the list of hotels had been returned from the server. Apologies if this is like pulling teeth ;)
 
You can register scripts with the scriptmanager itself. First make sure you are using the latest ajax dll.

function pageLoad(sender, args)
{
$find("IDOfExtender").add_populating(Populate);
}

function Populate(sender, args)
{
sender.get_element().disabled = true;
}

 
Try dropping an alert in this control after the checkforhotels and see when it pops up. I am not sure how that would run but if the ddl is populated before the alert then just call a function there and hide/show

<asp:DropDownList ID="dplCity" runat="server" onChange="CheckForHotels(this);alert('HERE')"/>
 
Sorry if I'm being a bit slow here, but how do I register the scripts with the script manager? Do you mean like this:
Code:
<asp:ToolkitScriptManager ID="TSM1" runat="server" [...] EnableViewState="true" EnablePartialRendering="true"  />
<asp:ScriptManagerProxy runat="server" >
     <Scripts>
         <asp:ScriptReference Path="~/scripts/myscriptfilenamehere.js" />
     </Scripts>
</asp:ScriptManagerProxy>
And then I put the two functions you mentioned in myscriptfilenamehere.js?

Also, the CheckForHotels function was the function I created to find the Hotels dropdown and see if it had any items - if no items found then it hides the dropdown and shows the text box, if items exist then it shows the dropdown and hides the textbox. But the major flaw was that this was happening before the hotels were populated. So I think the alert will always pop up before the dropdownlist was populated.

Sorry, I feel like there is something you're saying I'm not following. Could you briefly explain what your two functions would do and what event would cause them to run? I really appreciate your patience :)
 
(The code snippet in my last mail was from the Masterpage)
 
TSM1.RegisterClientScriptBlock(New TextBox, GetType(Object), "myScript", "script code here", True)
 
Wes, thanks very much for all your efforts to help me. I'm still not clear on a couple of things:

1. Why do you use "New Textbox" as the first (type) parameter?
2. What calls this Populate function and when?


Cheers,

Tom

 
populate is called on the load and the parameters I stuck in the function are just examples you would use your control. Check out some posts on RegisterClientScriptBlock and you should see how the parms are set. Hope you get it working :D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top