Skip to content

Asp - open dropdownlist by code

sara shats

Asked Jul 2, 2009, 3:43 AM EDT7 answers
Share
LinkedInX
hi,
can I open a dropdownlist of asp.net by server code?
(on asyncPostBack)
tanx.
sara.

6 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Niradhip Chakraborty
    0
    What do you mean by "opening dropdown list"
    0
  • Niradhip Chakraborty
    0
    Try this:
    Here I am showing it onmouseover.
    <asp:DropDownList id="DropDownList1" onmouseover="openListElement(this);" onmouseout="closeListElement(this);" runat="server">
     <asp:listitem value="">Select one</asp:listitem>
     <asp:listitem value="1">Item 1</asp:listitem>
     <asp:listitem value="2">Item 2</asp:listitem>
     <asp:listitem value="3">Item 3</asp:listitem>
     <asp:listitem value="4">Item 4</asp:listitem>
     <asp:listitem value="5">Item 5</asp:listitem>
     <asp:listitem value="6">Item 6</asp:listitem>
    </asp:DropDownList>

    <script type="text/javascript">
    <!--
    function openListElement(elementRef)
    {
     elementRef.size = elementRef.options.length;
    }
    function closeListElement(elementRef)
    {
     elementRef.size = 1;
    }
    // -->
    </script>

    You also can refer the following thread:
    http://forums.asp.net/p/1118333/1745578.aspx
    0
  • 0
    I'v tried it before, but I have to do it on server side only, after updating some fields.
    it's not going to client side after, I tryed to put ScriptManager.RegisterStartupScript, but it didnt do it
    (maybe its becose it's AsyncPostBack?)
    0
  • Niradhip Chakraborty
    0

    Use the following code in pagebehind where you want to call the function and write the javascript function in to of the page as it is shown below.

     

    Page.ClientScript.RegisterStartupScript(this.GetType(), "", " openListElement(this);";", true);

    // pass the instance of dropdownlist in onclick event

     

     

     

    %@ Page Language="C#" AutoEventWireup="true" CodeFile="Reservation.aspx.cs" ClientTarget="uplevel"

        Inherits="New_Reservation" EnableEventValidation="false" %>

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head id="Head1" runat="server">

        <title>New Page</title>

          <script type="text/javascript">

           function openListElement(elementRef)
                  {
                             elementRef.size = elementRef.options.length;
                   }

        </script>

     

    </head>

    <body bottommargin="0" topmargin="0" leftmargin="0" rightmargin="0">

        <form id="frmReservation" runat="server">

            <table cellpadding="0" cellspacing="0" border="0" width="100%">

                <tr valign="top">

                   <td align="right">

                    </td>

                 

             </table>

         </form>

    </body>

    0
  • 0
    I tryed:
    Page.ClientScript.RegisterStartupScript(This.GetType(), "", "openListElement(" + ddlUnder.ClientID + ");", True);
    and put debugger on the javascript function but it didn't get in.
    any idea?
    0
  • 0
    thanks for sharing this post it help full
    0
  • 1 short reply
    • 0
      like mouse click on it.
      0