1

i have a drop downlist. When Selected Index changes I wanted to handle it in javascript. So, as starting step , I tried to print the value of list item text in a textbox through javascript. But could not accomplish it successfully. Here is the dropdownlist:

       <asp:DropDownList Width="300px" ID="PlaceHoldersDropDownList" runat="server"
                AppendDataBoundItems="True" TabIndex="3" AutoPostBack="True" 
                OnSelectedIndexChanged = "PlaceHoldersDropDownList_SelectedIndexChanged"  >
            <asp:ListItem Value="">Select</asp:ListItem>
            <asp:ListItem Value="ContactName">[Contact Name]</asp:ListItem>
            <asp:ListItem Value="ProductName">[Product Name]</asp:ListItem>
            <asp:ListItem Value="ProductShortName">[Product Short Name]</asp:ListItem>
            <asp:ListItem Value="CurrentTime">[Current Time]</asp:ListItem>
            <asp:ListItem Value="EventStartTime">[Event Start Time]</asp:ListItem>
            <asp:ListItem Value="EventStopTime">[Event Stop Time]</asp:ListItem>
        </asp:DropDownList>
        <asp:TextBox ID="tb" runat="server"></asp:TextBox>

Here is the C# code

            protected void PlaceHoldersDropDownList_SelectedIndexChanged(object sender, 
                                                            EventArgs e)
    {
        var text = PlaceHoldersDropDownList.SelectedItem.Text;

        string x = text;
        PlaceHoldersDropDownList.Attributes.Add("onchange", "javscript:PasteTextInEditor
                                                                        ('"+text+"')");

    }

Here is the javascript

       function PasteTextInEditor(text) {

        var x = document.getElementById("<%= tb.ClientID %>");
        x.value = text;                    }

Can you please let me know the mistake I've been doing?

2
  • What happens if you simply alert the value? Commented May 22, 2012 at 14:27
  • the alert is not getting called. i think its not going into script Commented May 22, 2012 at 14:29

2 Answers 2

1

first you have to set AutoPostBack to false to handle it in client side(javascript) and you don't need to add onchange event programatically, you can just write it in the <asp:DrobDownList> something like that

<asp:DropDownList Width="300px" ID="PlaceHoldersDropDownList" runat="server"
     AppendDataBoundItems="True" TabIndex="3" AutoPostBack="false"
     onchange="PasteTextInEditor()">

and the PasteTextInEditor method will become

function PasteTextInEditor() {
    var text = $("#<%= PlaceHoldersDropDownList.ClientID %> option:selected").text();
    $("#<%= tb.ClientID %>").val(text);
}

note I am using jquery syntax

Sign up to request clarification or add additional context in comments.

4 Comments

The code lies in usercontrol. The error is coming from the parent page.
is this compilation error or runtime exception? I cannot get what causing this error without showing us the code, please provide more code to be able help you
i removed everything else and just kept the code but it still throws this error. I just have this entire code in usercontrol and i call this user control in another page
I created a sample code for what you want to do, you can download it here let me know if it doesn't help you
0

Using jQuery you can make the following:

1- turn off AutoPostBack and don't handle the OnSelectedIndexChanged event:

<asp:DropDownList Width="300px" ID="PlaceHoldersDropDownList" runat="server" AppendDataBoundItems="True" TabIndex="3" >

2- add a reference to jQuery

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>

3- add a "startup" script to hook the onchanged event for the dropdown list, read the javascripts comments for more details.

<script type="text/javascript">
    $(function () {
        var ddl = $("#<%= PlaceHoldersDropDownList.ClientID %>");
        var txt = $("#<%= tb.ClientID %>");

        // hook the change event for the drop down list
        $(ddl).change(function (e) {
            var selectedValue = $(ddl).val();

            // set the selectedValue into the textBox
            $(txt).val(selectedValue);
        });
    });
</script>

3 Comments

Hey. it doesn't work. I dont know jquery but how will script fire because i did not specify anything to run this script
It's fired when the document is ready, and the script binds a function to the onchanged event of the ddl. Then the function we passed by parameter to the changed method is executed when the onchange event is fired. Please download the working sample I wrote for you here: bit.ly/LyHbQ3
Thank you.. I'll look into it and try figuring out the problem

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.