Home All Groups Group Topic Archive Search About

dropdown list post back not working

Author
5 Jul 2006 1:27 PM
rak
I have a drop downlist, with AutoPostBack set to true.

<asp:DropDownList ID="ddlSettlementMethod" runat="server" DataSource="<%
#SettlementMethods%>" DataTextField="Code" DataValueField="Code"
AutoPostBack="true" style="width : 100%;"
OnSelectedIndexChanged="ddlSettlementMethod_SelectedIndexChanged" />


I also assign an onChange javascript handler as follows.

document.all('<%#ddlSettlementMethod.ClientID%>').onchange = function()
{
    var answer=confirm("Generate Default Instructions?")
    if(answer)
    {
        oldSettlementMethodSelectedIndex = this.selectedIndex;
    }
    else
    {
        this.selectedIndex = oldSettlementMethodSelectedIndex;
        event.cancelBubble = true;
        event.returnValue = false;
    }
};

however if confirm is true the ddlSettlementMethod_SelectedIndexChanged
doesn't get called.
does anyone know why this is?

Author
5 Jul 2006 2:14 PM
Alessandro Zifiglio
hi, this is happening because you are replacing the original method
referenced by the onchange function with your own custom annonymouse
function. If you really need to do this then try to do it like this :

Declarative code :
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DropDownList ID="DropDownList1" runat="server"
AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem>1</asp:ListItem>
            <asp:ListItem>2</asp:ListItem>
            <asp:ListItem>3</asp:ListItem>
        </asp:DropDownList>
        </div>
    </form>
<script language="javascript">

  <!--
  function myCustomJsFunction()
  {
    alert('fired');

  }
  -->

</script>

</body>


code in your codebehind :

protected void Page_Load(object sender, EventArgs e)
    {
        DropDownList1.Attributes.Add("onchange", "myCustomJsFunction();");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender,
EventArgs e)
    {
        Response.Write("SelectedIndexChanged");
    }

Note the usage of the attributes.add method to add a custom onchange
function call. What this will do is append your function call in there, so
the render of your dropdownlist will look like this :
<select name="DropDownList1"
onchange="myCustomJsFunction();setTimeout('__doPostBack(\'DropDownList1\',\'\')',
0)" id="DropDownList1">
<option value="1">1</option>
<option value="2">2</option>
<option selected="selected" value="3">3</option>

</select>

So now your custom function will be triggered before the postback method, i
guess this is what you are trying to achieve in the first place :-)

Also if your confirm dialogbox should return false, the onchange will be
cancelled and no postback will occur, while a confirm of true will trigger
the next function call(the original postback function).
Regards,
Alessandro Zifiglio
http://www.AsyncUI.net

Show quoteHide quote
"rak" <u23774@uwe> ha scritto nel messaggio news:62cb0f0f5a0a1@uwe...
>I have a drop downlist, with AutoPostBack set to true.
>
> <asp:DropDownList ID="ddlSettlementMethod" runat="server" DataSource="<%
> #SettlementMethods%>" DataTextField="Code" DataValueField="Code"
> AutoPostBack="true" style="width : 100%;"
> OnSelectedIndexChanged="ddlSettlementMethod_SelectedIndexChanged" />
>
>
> I also assign an onChange javascript handler as follows.
>
> document.all('<%#ddlSettlementMethod.ClientID%>').onchange = function()
> {
>    var answer=confirm("Generate Default Instructions?")
>    if(answer)
>    {
>        oldSettlementMethodSelectedIndex = this.selectedIndex;
>    }
>    else
>    {
>        this.selectedIndex = oldSettlementMethodSelectedIndex;
>        event.cancelBubble = true;
>        event.returnValue = false;
>    }
> };
>
> however if confirm is true the ddlSettlementMethod_SelectedIndexChanged
> doesn't get called.
> does anyone know why this is?
Author
5 Jul 2006 3:49 PM
rak
Hello Alessandro,

so I now have this...

<asp:DropDownList ID="ddlSettlementMethod" runat="server" DataSource="<%
#SettlementMethods%>" DataTextField="Code" DataValueField="Code"
AutoPostBack="true" style="width : 100%;"
OnSelectedIndexChanged="ddlSettlementMethod_SelectedIndexChanged" />

function onChangeSettlementMethod()
{
alert('index ' + document.all('<%#ddlSettlementMethod.ClientID%>').
selectedIndex)
};

in my code behind I have

        protected void Page_Load(
            object sender,
            EventArgs e
            )
        {
            ddlSettlementMethod.Attributes.Add("onchange",
"onChangeSettlementMethod();");
            if (IsPostBack)
            {
                PopulateSession();
            }
        }

and

        protected void ddlSettlementMethod_SelectedIndexChanged(object sender,
EventArgs e)
        {
            Response.Write("SettlementMethod Changed");
            if (ddlSettlementMethod.SelectedIndex != -1)
            {
                if (SettlementMethods[ddlSettlementMethod.SelectedIndex] !=
_trade.Transaction.SettlementMethod)
                {
                    _trade.Transaction.UpdateSettlementMethod
(SettlementMethods[ddlSettlementMethod.SelectedIndex]);
                }
            }

        }

however the SelectedIndexChanged handler doesn't get called.
The page_load and pre_render methods are called and if I use debug
ddlSettlementMethod.SelectedIndex seems to be set to -1 when I'm inside
page_load.after changing the index. However the alert shows the index I
selected

thanks
Rak
Author
5 Jul 2006 4:08 PM
Alessandro Zifiglio
Rak, what is that stuff you have in your js with the databinder syntax, also
your js code needs to be put in js code-blocks as in my example i posted
earlier.
Also you dont need to do a document.all. You can just refer to the current
dropdownlist that raised the function via the this keyword. so :
<script type="text/javascript">
  <!--
function onChangeSettlementMethod()
{
        alert('index ' + this.options[this.selectedIndex].value);
}
-->
</script>

Regards,
Alessandro Zifiglio
http://www.AsyncUI.net

Show quoteHide quote
"rak" <u23774@uwe> ha scritto nel messaggio news:62cc4d6acb527@uwe...
> Hello Alessandro,
>
> so I now have this...
>
> <asp:DropDownList ID="ddlSettlementMethod" runat="server" DataSource="<%
> #SettlementMethods%>" DataTextField="Code" DataValueField="Code"
> AutoPostBack="true" style="width : 100%;"
> OnSelectedIndexChanged="ddlSettlementMethod_SelectedIndexChanged" />
>
> function onChangeSettlementMethod()
> {
> alert('index ' + document.all('<%#ddlSettlementMethod.ClientID%>').
> selectedIndex)
> };
>
> in my code behind I have
>
>        protected void Page_Load(
>            object sender,
>            EventArgs e
>            )
>        {
>            ddlSettlementMethod.Attributes.Add("onchange",
> "onChangeSettlementMethod();");
>            if (IsPostBack)
>            {
>                PopulateSession();
>            }
>        }
>
> and
>
>        protected void ddlSettlementMethod_SelectedIndexChanged(object
> sender,
> EventArgs e)
>        {
>            Response.Write("SettlementMethod Changed");
>            if (ddlSettlementMethod.SelectedIndex != -1)
>            {
>                if (SettlementMethods[ddlSettlementMethod.SelectedIndex] !=
> _trade.Transaction.SettlementMethod)
>                {
>                    _trade.Transaction.UpdateSettlementMethod
> (SettlementMethods[ddlSettlementMethod.SelectedIndex]);
>                }
>            }
>
>        }
>
> however the SelectedIndexChanged handler doesn't get called.
> The page_load and pre_render methods are called and if I use debug
> ddlSettlementMethod.SelectedIndex seems to be set to -1 when I'm inside
> page_load.after changing the index. However the alert shows the index I
> selected
>
> thanks
> Rak
Author
6 Jul 2006 10:23 AM
rak via DotNetMonster.com
Hello,
I think the problem is the handler is part of a WebUser control and the
control is dynamically created.
I seen a couple things that mention using the attribute
[ViewStateModeById]. Which I've added to the user controls. But this doesn't
seem to be working

regards
Rak