ASP.NET自定义控件属性访问

时间:2022-09-02 13:29:02

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

我有一个名为'FileBrowser'的用户控件。该控件包含一个名为“FileList”的ListBox。后面的代码公开了一个属性:

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.

我看到一些非常奇怪的行为。当我单击按钮textBoxTest时,我从上次单击按钮时获取SelectedValue的值。

Example:

例:

FileList.SelectedPath = Test1

FileList.SelectedPath = Test1

click returns "empty"

点击返回“空”

click again, now it returns "Test1"

再次单击,现在返回“Test1”

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

在列表框上选择一个新值test2,再次单击,返回“Test1”

Click again, returns "test2"

再次单击,返回“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.

我对ASP.NET和Web开发一般都很陌生。我想也许有一些奇怪的生命周期事件发生,我不熟悉。

1 个解决方案

#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.

选择列表框值时,它会在客户端更改。但是您使用服务器端代码来获取值,' '仍然是'空'(初始值),直到页面被回发。 (filebrowser.selectedpath);%>

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

在用户控件中,如果为ListBox设置AutoPostBack =“True”,您将获得所需的结果。

#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.

选择列表框值时,它会在客户端更改。但是您使用服务器端代码来获取值,' '仍然是'空'(初始值),直到页面被回发。 (filebrowser.selectedpath);%>

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

在用户控件中,如果为ListBox设置AutoPostBack =“True”,您将获得所需的结果。