我正在编写一个新的用户控件下拉列表,我的代码是
<%@ 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();
}
当我使用这个控件时它工作正常,除了它的seletected索引总是0并且所选值总是为null我如何维护它的索引和值
答案 0 :(得分:-1)
对于用户控件,有两种选择可以保持状态:
用户控件属性只是包含在其中的控件属性的包装(保持其状态)
用户控件保持其自己的状态
在这种情况下,用户控件(它更像是另一个控件的包装器)正在从get {}中的下拉列表中读取一些属性,但它没有将它们设置回集{}。一种选择是:
public string SelectedVal
{
get
{
_selectedvalue = this.ddlStytlish.SelectedValue;
return _selectedvalue;
}
set
{
_selectedvalue = value;
this.ddlStytlish.SelectedValue = value;
}
}
另一个选项是保留用户控件的属性,如本视频中所述: