Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > determining a dropdownlist selected item without doing a postback

Reply
Thread Tools

determining a dropdownlist selected item without doing a postback

 
 
SGSmith
Guest
Posts: n/a
 
      07-05-2004
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
 
Reply With Quote
 
 
 
 
Ken Cox [Microsoft MVP]
Guest
Posts: n/a
 
      07-05-2004
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>
<aspropDownList 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>
</aspropDownList></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>


"SGSmith" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help to fill textboxes from a selected item in datagrid-selected index changed. mldardy ASP .Net 0 09-28-2010 02:59 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net Web Controls 0 10-05-2008 10:29 PM
Selected ListItem in DropDownList does not appear selected Nathan Sokalski ASP .Net 0 10-05-2008 10:29 PM
DropDownList 2 always returns Selected = 0 for all items - even selected item Iain ASP .Net 3 12-11-2006 11:07 AM
Determining item count of databound DropDownList Kyle K. ASP .Net Web Controls 2 06-08-2006 05:32 PM



Advertisments