Click to See Complete Forum and Search --> : dropdownlist AutoPostBack="True" doenst work


drewex
12-17-2004, 04:32 AM
Hi all,

This problem is bothering me for a while. I have a drop down list that has one autopostback open but it doenst call the function anymore. here is the codes

<asp:dropdownlist id="ShippingType" runat="server" AutoPostBack="True">
<asp:ListItem Value="0">Select Shipping Option</asp:ListItem>
</asp:dropdownlist></TD>

and the vb code

Private Sub ShippingType_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShippingType.SelectedIndexChanged

and on the load


ShippingType.Items.Add(New ListItem(Rs("ShippingType") & " " & Convert.ToDouble(Rs("amt")).ToString("C"), Convert.ToDouble(Rs("amt"))))


so i add more items to it on the load of the page.
even if i had items in it hard coded it still doesnt call the SelectedIndexChanged function what am i doing wrong.

OO by the way this is for asp.net

update:

onchange="javascript:UpdateCor()__doPostBack('ShippingType','')"
has this code in the html code if this helps to solve the problem.

dtch
12-17-2004, 05:30 PM
Hi Drewex,

You are not alone.
I have the same problem.

I experience the same behavior on three webforms.
Finally I got tired of fighting this with ASP.Net and solved my problem using JavaScript.

What did I do?

In the form_load event I add this line of code:

ddlProblematicOne.Attributes.Add("onchange", "return selectIndex(this);")

And in the "head" of the html document I put this code:

<script language="JavaScript">
function selectIndex(obj) {
document.getElementById("selIndex").value = obj.selectedIndex;
document.Form1.submit();
}

</script>

where "selIndex" is the id of a hidden input field which I put in my form:

<INPUT id="selIndex" type="hidden" value="0" name="selIndex" runat="server">

I need to use this hidden field, because even if I cause the autopostback using JavaScript, the control will not remember which was the selected index, and would select always the first one.

Then in the form_load event I would put this line to tell the dropdown which is the selected item:

ddlProblematicOne.SelectedIndex = CType(selIndex.Value, Integer)

Good Luck!

Let us know if you find the solution through the code behind.

Cstick
12-18-2004, 08:34 AM
I have a drop down list that has one autopostback open but it doenst call the function anymore.

Just to make sure Drewex, your browser does still have JS enabled right? That is the only time I have noticed the autopostback doesn't work like I expected.

drewex
12-20-2004, 11:49 AM
Hi no my javascript is open i do use it a lot. But dtch code did work. It helped alot and i got it done. Thanks

webdev5
08-30-2007, 03:54 PM
Will you give me an example of the form load to which you are referring?

This is what I came up with and its obviously wrong:

Private Sub Form_Load()
ddlProblematicOne.Attributes.Add("onchange", "return selectIndex(this);")
ddlProblematicOne.SelectedIndex = CType(selIndex.Value, Integer)
End Sub

dtch
08-30-2007, 04:50 PM
It has been a while.

What version of Visual Studio are you using: 2003 or 2005. I don't have the problem with 2005.

If you are using master pages then the JavaScript portion will not work.

The Form_Load even is the Code Behind server side event, and here is an example of where the code goes:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

ddlDistrictLayers.Attributes.Add("onchange", "return selectIndex(this);")

If Not Me.IsPostBack Then
Dim intSelectedIndex As Integer = CType(selIndex.Value, Integer)
ddlDistrictLayers.SelectedIndex = intSelectedIndex
'Do something here like populating a different dropdown with data depending on the selection of the above dropdown
End If


End Sub


And if you are using master pages, you can try something like this to make the Javascript work:

function selectIndex(obj) {
var control = $get('<%=selIndex.ClientID%>');
control.value = obj.selectedIndex;
document.Form1.submit();
}


And the hidden field looks like this:

<INPUT id="selIndex" type="hidden" value="0" name="selIndex" runat="server">


Hope this helps, but if it doesn't post some code, so we can troubleshoot.

Good Luck!

webdev5
08-30-2007, 05:22 PM
Thanks for your help,

'I. Parent Page------

'A. Javascript
function validateForm() {
if (entry.pubdate.value == "") {
alert("Please enter a publication date");
document.entry.pubdate.focus();
return false;
}
if (entry.slug.value == "") {
alert("Please enter a slug");
document.entry.slug.focus();
return false;
}
return true;
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
return false;
}
function selectIndex(obj) {
document.getElementById("selIndex").value = obj.selectedIndex;
document.entry.submit();
}

'B. html

<body bgcolor="#FFFFFF" onLoad="MM_preloadImages('images/Plus_hover.gif')">
<form name="entry" method="POST" action="multimedia_entry.asp">
<INPUT id="selIndex" type="hidden" value="0" name="selIndex" runat="server">

<tr>
<td align="right" valign="top">Assignee</td>
<td><select name="assignee" id="assigneeDropdown" AutoPostBack="true">
<option value="0">-- Assign a person --</option>
<% do while not StaffResult.eof %>
<option value="<%= StaffResult("emp_id")%>"><%= StaffResult("fname") & " " & StaffResult("lname")%></option>
<% StaffResult.movenext
loop %>
</select>

<input name="plus" type="image" id="plus" onClick="return MM_openBrWindow('new_assignee.asp','newAssignee','width=500,height=200')" onMouseOver="MM_swapImage('plus','','images/Plus_hover.gif',1)" onMouseOut="MM_swapImgRestore()" src="images/Plus.gif">
</input></td>
</tr>

'End of Parent Snippets

'Child

javascript functions

function addAssignee() {
//window.opener.location.reload();
//window.opener.location.reload(); //works in FF

//window.parent.location.reload(true); //doesn't reload the page

//document.location.href = document.location.href;
//window.opener.location.href = window.opener.location.href;
//window.opener.location.reload = window.opener.location.reload;
//document.location;
//opener.window.location.reload();
//opener.location.reload(true);

//window.opener.parent.location.reload();

//window.opener.document.location.reload();
window.opener.location.href='multimedia_entry.asp'; //refreshes in IE7 but not in the dropdown
window.close();
//self.close();
}

<body bgcolor="#FFFFFF" onLoad="document.newAssignee.fname.focus()">
<form name="newAssignee" method="POST" action="new_assignee.asp" target="_parent">
<table width="500" border="0" cellspacing="0" cellpadding="4">
<tr>
<td align="right" valign="top">First Name</td>
<td><input name="fname" type="text" id="fname"></td>
</tr>
<tr>
<td align="right" valign="top">Last Name</td>
<td><input name="lname" type="text" id="lname"></td>
</tr>
<tr>
<td align="right" valign="top">Phone</td>
<td><input name="phone" type="text" id="phone"></td>
</tr>
<tr>
<td align="right" valign="top">E-Mail</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="SubmitAssignee" value="Submit" onClick="addAssignee()"> <input type="reset" name="clearAssignee" value="Clear"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

lmf232s
08-31-2007, 09:09 AM
What you have should work. Here is an example I put together that works just fine so it may be some other code on your page. Also your javascript looks a little funny (from the first post). What is UpdateCor()? Its like your tring to call a javascript function before you post back. It also looks like it should be UpdateCor();__dopostback.

Any way it may have something to do with the way you are adding your items to he drop down (although I got it to working with the way your adding your items)


<asp:DropDownList ID="dlTest" runat="server" AutoPostBack="true">
<asp:ListItem Value="" Text="--" />
</asp:DropDownList>

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
For i As Integer = 1 To 10
With Me.dlTest
'This is they way your adding your items. Notice there is no index
'.Items.Add(New ListItem("Test"))

'This method allows you to give your item an index. Which without could be causing problems.
.Items.Insert(i, New ListItem("Item-" & i, "Item-" & i))
End With
Next
End If
End Sub

Protected Sub dlTest_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dlTest.SelectedIndexChanged
Response.Write(" i made it")
End Sub

dtch
08-31-2007, 10:05 AM
Hi Webdev5,

The only problem I see in your code is that you are using an HTML select control, which is not running on the server, instead of a server dropdown control. That's why you cannot access this control from your Form_Load server side code.

Use a server dropdownlist control as Imf232s has shown.

I would suggest trying first the dropdownlist AutoPostBack = true, and see if this works for you without the JavaScript solution. If it doesn't, then use it.

Good Luck!