Michael
Michael

Reputation: 1833

ASP.NET Custom Control Property Access

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.

Upvotes: 1

Views: 144

Answers (1)

afzalulh
afzalulh

Reputation: 7943

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.

Upvotes: 1

Related Questions