How do I load a list box on demand?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • E11esar
    New Member
    • Nov 2008
    • 132

    How do I load a list box on demand?

    Hi there.

    Within my ASP.Net (C#) page I have a Panel which contains besides other controls, a ListBox.

    This Panel is invoked by a ModalPopupExten der and when it is invoked, I'd like to populate the ListBox from an external web method. At the moment I have the Panel OnLoad calling a method which returns a DataSet that populates the ListBox, but at the moment this event runs during the Web Page Load and I was hoping this would only load when the Panel was loaded - if that makes sense..?

    Hence what I am after is how to populate the ListBox within the ModalPopup's Panel control? Any help please as this one has me going in bit of a circle.

    Thank you.

    Mark
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    You're starting to deal with the same things that I'm dealing with these days... although my control is a bit more complicated than what you're describing... I'm actually working on something very similar.

    I'll let you know how I'm currently achieving this, however I haven't worked out all the kinks in every browser yet so when you hit the same problems that I'm having maybe you can help me ;)

    First of all, make sure that you have your content within an UpdatePanel...o therwise the whole page will be updated instead of just the section that you want to be updated (the section containing the ListBox that is).

    Add a button to this UpdatePanel and set it's style to "display:none;" ....give it an ID and text...and call it something like "UpdateListBox" . In the code that handles this button, do what is necessary to update the ListBox.

    Before you display your modular extender, you'll have to cause the UpdateListButto n to post back to the server. To do this you'll need to add a JavaScript function to the page:

    Code:
    function UpdateListBox(){
        __doPostBack('<%=UpdateListBox.ClientID %>','');
    }
    The above code calls the ASP.NET __doPostBack function that will post back to the server. You pass it 2 parameters: the eventTarget, and eventArgument.. ..in the above code you are passing it the ID of the UpdateListBox button as the eventTarget and nothing for the eventArgument.

    (Please note that '<%= %>' is ASP short hand for the Response.Write( ) method..therefo re this is actually executed server side. It writes the ClientID of the UpdateListBox button in its place when the HTML content is sent to the browser).

    I use the __doPostBack method because it determines whether or not to do a full page postback or a partial page postback. There is another way to do this using the ASP.NET Ajax Framework/Class libraries but I'm not going to get into that because it's more restricting.


    Now that you have a button within an UpdatePanel, and JavaScript that posts back to the server indicating that the UpdateListBox button is the eventTarget for the postback.....an d the UpdateListBox button updates the ListBox....all you have to do is call the JavaScript function before the ModalPopupExten der displays the panel.


    So, in a nutshell, that's the technique I'm using.

    If you are using a pure WebMethod then you are going to have to do research on how to create a JavaScript proxy class in order to call the WebMethod...... .which is more complicated and something that I haven't tried doing yet...but there's lots of information out there on the web that describes how to do this.


    Cheers!

    -Frinny

    Comment

    • E11esar
      New Member
      • Nov 2008
      • 132

      #3
      Response

      Thank you, Frinny.

      I'll give that a go - hopefully this will address matters as this has been a painful one to try and catch.

      M :o)

      Comment

      Working...