determining a dropdownlist selected item without doing a postback

S

SGSmith

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
 
K

Ken Cox [Microsoft MVP]

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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top