Help with dropdownlist

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nomad

    Help with dropdownlist

    Hi,

    I have a databound dropdownlist. I have added
    AppendDataBound Items="True" so that I am able to enter a item in the
    dorpdownlist of "-- Please select --". I would like to be able to set
    it so that this value isn't selectable because as it stands if they
    select that value then my applicaiton will fall over.

    Appreciate any help on this.

    Thanks
  • Mark Rae [MVP]

    #2
    Re: Help with dropdownlist

    "nomad" <d.bedgood@o2.c o.ukwrote in message
    news:a792637b-7121-4f10-84e7-f5b7043d2814@p2 5g2000hsf.googl egroups.com...
    I have a databound dropdownlist. I have added
    AppendDataBound Items="True" so that I am able to enter a item in the
    dorpdownlist of "-- Please select --". I would like to be able to set
    it so that this value isn't selectable because as it stands if they
    select that value then my applicaiton will fall over.
    Your application will not fall over if you write it properly.

    Specifically, in this case, you need to start using validation on your
    postbacks.

    If it's not valid for a user to submit the form without selecting one of
    "real" databound items in the DropDownList, then don't allow them to do
    that.

    E.g.

    <script type="text/javascript">
    function validateForm()
    {
    var myDropDownList =
    document.getEle mentById('<%=My DropDownList.Cl ientID%>');
    if (myDropDownList .selectedIndex < 1)
    {
    alert ('Please select an option');
    myDropDownlList .focus();
    return false;
    }
    }
    </script>

    <asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
    OnClick="MySubm itButton_Click" OnClientClick = "return validateForm(); " />


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • nomad

      #3
      Re: Help with dropdownlist

      On Aug 11, 10:37 am, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
      "nomad" <d.bedg...@o2.c o.ukwrote in message
      >
      news:a792637b-7121-4f10-84e7-f5b7043d2814@p2 5g2000hsf.googl egroups.com...
      >
      I have a databound dropdownlist.  I have added
      AppendDataBound Items="True" so that I am able to enter a item in the
      dorpdownlist of "-- Please select --".  I would like to be able to set
      it so that this value isn't selectable because as it stands if they
      select that value then my applicaiton will fall over.
      >
      Your application will not fall over if you write it properly.
      >
      Specifically, in this case, you need to start using validation on your
      postbacks.
      >
      If it's not valid for a user to submit the form without selecting one of
      "real" databound items in the DropDownList, then don't allow them to do
      that.
      >
      E.g.
      >
      <script type="text/javascript">
          function validateForm()
          {
              var myDropDownList =
      document.getEle mentById('<%=My DropDownList.Cl ientID%>');
              if (myDropDownList .selectedIndex < 1)
              {
                  alert ('Please select an option');
                  myDropDownlList .focus();
                  return false;
              }
          }
      </script>
      >
      <asp:Button ID="MySubmitBut ton" runat="server" Text="Submit"
      OnClick="MySubm itButton_Click" OnClientClick = "return validateForm(); " />
      >
      --
      Mark Rae
      ASP.NET MVPhttp://www.markrae.net
      Hi Mark,

      Thanks for the reply. I thought there may have been a setting which
      allows you to set the first item as unselectable, without writing a
      method to catch this.

      Comment

      Working...