determining a dropdownlist selected item without doing a postback

Discussion in 'ASP .Net Web Controls' started by SGSmith, Jul 5, 2004.

  1. SGSmith

    SGSmith Guest

    I'm trying to find a way to determine the value of a dropdownlist without forcing a postback. The dropdown is actually a template column in a datagrid and I need to find out what the value is before posting back to the server. When the user selects an associated button column, I want to determine determine what the dropdownlist's selectedvalue is. What gets returned though is the initial value.

    If I use the following on the datagrid_itemcommand event:

    sString = CType(e.Item.FindControl("ddFieldName"), DropDownList).SelectedValue

    .... it returns the selectedvalue prior to being changed by the user. Without the dropdownlist being set to autopostback, it does not appear to pick up that a user has in fact changed the control.

    Is there a way to access the changed selectedvalue without forcing the control to postback to the server?

    Thanks for any help you can provide,

    Stephen Smith
    VB .Net
     
    SGSmith, Jul 5, 2004
    #1
    1. Advertising

  2. He Stephen,

    Maybe the code below will give you some ideas?

    It jams in some client-side script that checks the dropdownlist's value. If
    it is "0", the routine returns false so the server-side button click doesn't
    fire.

    Perhaps someone else can build on this?

    Ken
    Microsoft MVP [ASP.NET]


    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles MyBase.Load
    ' By Ken Cox [MVP]
    ' for Stephen Smith
    ' July 5, 2004
    Dim sb As New System.Text.StringBuilder
    sb.Append("<script language='javascript'>")
    sb.Append("function checkddl()")
    sb.Append("{")
    sb.Append("var selNum=document.forms[0].")
    sb.Append("DropDownList1.selectedIndex;")
    sb.Append("var selVal = document.forms[0].")
    sb.Append("DropDownList1.options[selNum].value;")
    sb.Append("alert(selVal);")
    sb.Append("if (selVal == '0')")
    sb.Append("{")
    sb.Append(" alert('Select Something Else!');")
    sb.Append(" return false")
    sb.Append("}")
    sb.Append("else")
    sb.Append("{")
    sb.Append("return true")
    sb.Append("}")
    sb.Append("}")
    sb.Append("</script>")
    Page.RegisterClientScriptBlock _
    ("checkddl", sb.ToString)
    Button1.Attributes.Add _
    ("onclick", "return checkddl();")
    End Sub

    Private Sub Button1_Click _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) _
    Handles Button1.Click
    Label1.Text = "At " & Now.ToLongTimeString & _
    ", The selected value was: " & _
    DropDownList1.SelectedValue.ToString
    End Sub

    <P>
    <asp:DropDownList id="DropDownList1" runat="server">
    <asp:ListItem Value="0">--Select--</asp:ListItem>
    <asp:ListItem Value="Red">Red</asp:ListItem>
    <asp:ListItem Value="Green">Green</asp:ListItem>
    <asp:ListItem Value="Blue">Blue</asp:ListItem>
    </asp:DropDownList></P>
    <P>
    <asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
    <P>
    <asp:Label id="Label1" runat="server"></asp:Label></P>


    "SGSmith" <> wrote in message
    news:...
    > I'm trying to find a way to determine the value of a dropdownlist without
    > forcing a postback. The dropdown is actually a template column in a
    > datagrid and I need to find out what the value is before posting back to
    > the server. When the user selects an associated button column, I want to
    > determine determine what the dropdownlist's selectedvalue is. What gets
    > returned though is the initial value.
    >
    > If I use the following on the datagrid_itemcommand event:
    >
    > sString = CType(e.Item.FindControl("ddFieldName"),
    > DropDownList).SelectedValue
    >
    > ... it returns the selectedvalue prior to being changed by the user.
    > Without the dropdownlist being set to autopostback, it does not appear to
    > pick up that a user has in fact changed the control.
    >
    > Is there a way to access the changed selectedvalue without forcing the
    > control to postback to the server?
    >
    > Thanks for any help you can provide,
    >
    > Stephen Smith
    > VB .Net
     
    Ken Cox [Microsoft MVP], Jul 6, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Iain
    Replies:
    3
    Views:
    959
  2. Nathan Sokalski
    Replies:
    0
    Views:
    643
    Nathan Sokalski
    Oct 5, 2008
  3. mldardy
    Replies:
    0
    Views:
    991
    mldardy
    Sep 28, 2010
  4. Kyle K.

    Determining item count of databound DropDownList

    Kyle K., Jun 8, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    695
    Kyle K.
    Jun 8, 2006
  5. Nathan Sokalski

    Selected ListItem in DropDownList does not appear selected

    Nathan Sokalski, Oct 5, 2008, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    308
    Nathan Sokalski
    Oct 5, 2008
Loading...

Share This Page