2

Hello i trying to change ddl.selectedIntex=1,i see that value of ddl is changing but event ddl.change did't work.

$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts")[0].selectedIndex = 1; 

This just set the value but i need to run the .change function

any ideas? I try this in console all works thx,meybe you know how can i do this by passing the PostData to the HttpWebRequest?

I try this in console all works thx,meybe you know how can i do this by passing the PostData to the HttpWebRequest I added my code

HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create("https://www.com/Pages/current.aspx");
PostString += "ctl00$PlaceHolderMain$AccountsDDL$ddlAccounts=" + Number + "&";//Here
 byte[] byteArray = Encoding.ASCII.GetBytes(PostString);
    postRequest.ContentLength = byteArray.Length;
    Stream newStream = postRequest.GetRequestStream();
    newStream.Write(byteArray, 0, byteArray.Length);
    newStream.Close();
    HttpWebResponse postResponse = (HttpWebResponse)postRequest.GetResponse();
2
  • 1
    use __doPostBack('ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts',''); or $("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts").change() Commented Apr 7, 2014 at 9:30
  • I try this in console all works thx,meybe you know how can i do this by passing the PostData to the HttpWebRequest? Commented Apr 7, 2014 at 9:35

3 Answers 3

4

The change event is not triggered when value is changed through code. You need to call change() or trigger("change")

$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts").change()

or

$("#ctl00_PlaceHolderMain_AccountsDDL_ddlAccounts").trigger("change")

As a additional note use the Control.ClientID to get the element in javascript instead of using the hard coded asp.net generated client id.

$("#<%= ddlAccounts.ClientID %>").change()
Sign up to request clarification or add additional context in comments.

7 Comments

Adil is right, change event is not fired in case that value is changed programmatically (by javascript).
$('[id$="ddlAccounts"]') can also be used to get the element.
Yes, but I think that is less efficient way as it will have to search for matching element instead of directly getting the element.
$("#<%= ddlAccounts.ClientID %>") will not work if the scripts are placed in .js file. In that case $("#dllAccounts") can be used provided with attribute ClientIdMode = "static" for the DropDownList or $('[id$="ddlAccounts"]') can be used if ClientIdMode property is not set.
Can you help me with sending data to HttpWebRequest i added new code on top
|
0

or just use this

$("[id$=ddlAccounts]").change();

use of $ 'Ends with' to shorten the name of selector

Comments

0
 <script>
        $("select").change(function () {
            var str = "";
            $("select option:selected").each(function () {
                str += $(this).text() + " ";
                $("#DropDownList1")[0].selectedIndex = 5;
            });
            $("div").text(str);
        }).change();

    </script>
    <div></div>

Try this.... it may help you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.