0

我正在编码一个新的用户控件下拉我的代码是

        <%@ Control Language="C#" AutoEventWireup="true" CodeFile="StylishDDL.ascx.cs" Inherits="JDropDown.Controls_StylishDDL" %>   
     <script src="Jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
        <script src="Jquery/jquery.selectbox-0.5.js" type="text/javascript"></script> 
        <script type="text/javascript">
                    $(document).ready(function () {
                        $('#ddlStytlish').selectbox();
                    });
        </script>
        <asp:DropDownList ID="ddlStytlish" runat="server" ClientIDMode="Static">
        </asp:DropDownList>

namespace JDropDown
{
    public partial class Controls_StylishDDL : UserControl
    {
        public string _selectedvalue = "";
        public int _selectedIndex = 0;
        public Controls_StylishDDL( )
        {

        }
        public Controls_StylishDDL(int selectedIndex , string selectedval)
        {
            SelectedIndx = selectedIndex;
            SelectedVal = selectedval;
        }

        public string SelectedVal
        {
            get
            {
                _selectedvalue = this.ddlStytlish.SelectedValue;
                return _selectedvalue;
            }
            set { _selectedvalue = value; }
        }

        public int SelectedIndx
        {
            get
            {
                _selectedIndex = this.ddlStytlish.SelectedIndex;
                return this._selectedIndex;
            }

            set { this._selectedIndex = value; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            this.ddlStytlish.AutoPostBack = false;
            LoadStates();
        }

当我使用此控件时,它工作正常,除了它的选定索引始终为 0 并且选定的值始终为空 我如何维护它的索引和值

4

1 回答 1

-1

对于用户控件,有两种选择来保持状态:

  1. 用户控件属性只是其中包含的控件属性的包装器(保持其状态)

  2. 用户控件保持自己的状态

在这种情况下,用户控件(它更像是另一个控件的包装器)正在从 get {} 的下拉列表中读取一些属性,但不会将它们设置回 set {}。一种选择是:

public string SelectedVal
{
    get
    {
        _selectedvalue = this.ddlStytlish.SelectedValue;
        return _selectedvalue;
    }
    set 
    { 
        _selectedvalue = value; 
        this.ddlStytlish.SelectedValue = value; 
    }
}  

另一种选择是保留用户控件的属性,就像本视频中解释的那样:

http://www.asp.net/web-forms/videos/how-do-i/how-do-i-persist-the-state-of-a-user-control-during-a-postback

于 2012-05-23T17:40:36.863 回答