1

I have a User Control called 'FileBrowser.' The control contains a ListBox called 'FileList.' The code behind exposes a property:

public string SelectedPath
    { get { return string.IsNullOrEmpty(FileList.SelectedValue) ? "empty" : FileList.SelectedValue; } }

I am accessing this from a page implementing the control using this:

<script>
    function testFunc() {
        var s = '<% Response.Write(fileBrowser.SelectedPath);%>';
        document.getElementById('<%= textBoxTest.ClientID %>').value = s;            
     }
</script>

I see some very strange behavior. When I click the button textBoxTest I get the value of SelectedValue from when the button was last clicked.

Example:

FileList.SelectedPath = Test1

click returns "empty"

click again, now it returns "Test1"

Select a new value on the listbox, test2, click again, returns "Test1"

Click again, returns "test2"

I'm very new to ASP.NET and web development in general. I suppose maybe there are some strange life cycle events occuring that I am not familiar with.

1 Answer 1

1

When you select a listbox value, it changes in client side. But you are using serverside code to get the value, '<% Response.Write(fileBrowser.SelectedPath);%>' which is still 'empty' (initial value), untill the page is posted back.

In the user control if you set AutoPostBack="True" for the ListBox, you will get desired result.

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

Comments

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.