简体   繁体   English

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

[英]ASP.NET Custom Control Property Access

I have a User Control called 'FileBrowser.' 我有一个名为“ FileBrowser”的用户控件。 The control contains a ListBox called 'FileList.' 该控件包含一个名为“ 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. 当我单击按钮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. 一般来说,我对ASP.NET和Web开发非常陌生。 I suppose maybe there are some strange life cycle events occuring that I am not familiar with. 我想也许发生了一些我不熟悉的奇怪的生命周期事件。

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. 但是,您正在使用服务器端代码来获取值'<% Response.Write(fileBrowser.SelectedPath);%>' ,该值仍为“空”(初始值),直到页面回发'<% Response.Write(fileBrowser.SelectedPath);%>'

In the user control if you set AutoPostBack="True" for the ListBox, you will get desired result. 在用户控件中,如果将ListBox设置为AutoPostBack="True" ,则将获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM