【发布时间】:2017-04-25 20:04:25
【问题描述】:
概述
我有一个 ASCX 用户控件,我正尝试将它用于我的 Web 应用程序。
控件有多个属性需要设置才能正常工作。
控件在 GridView 中使用。控件的每个实例都需要它所在行的数据。
我尝试了什么
我尝试使用属性和Eval 方法设置属性值来分配值。例如:
页面代码:
<cm:TestManagerEditor runat="server" id="TestManagerEditor" FilePath='<%# System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Some\\Path\\In\\The\\WebApp\\" + AccountYearPeriodOptionsGroupRandomTestManager.SelectedAccountValue + "\\" %>' />
我也尝试过设置RowDataBound 事件的值。例如:
页面代码:
private string PathToUserFiles = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Some\\Path\\In\\The\\WebApp\\";
protected void GridViewData_RowDataBound(object sender, GridViewRowEventArgs e)
{
// this is the customized path to files for the selected account.
string UserFilePath = PathToUserFiles + AccountYearPeriodOptionsGroupRandomTestManager.SelectedAccountValue + "\\";
// set modal dialog properties and add the scripting to open those dialogs.
if (e.Row.RowType == DataControlRowType.DataRow)
{
Controls_Modals_TestManagerEditor EditorModal = e.Row.FindControl("TestManagerEditor") as Controls_Modals_TestManagerEditor;
EditorModal.FilePath = UserFilePath;
}
}
问题
当我从控件打开的页面访问我使用上述任一方法设置的属性时,值会正确返回。但是,如果我试图从控件的代码隐藏中访问属性的值,它会返回属性的默认值(通常是 NULL 或 string.Empty),而不是设置的值。
例如,上面使用的FilePath 属性的声明与其他任何属性一样:
用户控制代码:
/// <summary>
/// The path to the location of the uploaded files.
/// </summary>
private string _FilePath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Some\\Path\\In\\The\\WebApp\\";
/// <summary>
/// Gets or sets the path to the location of uploaded files.
/// </summary>
public string FilePath
{
get
{
return _FilePath;
}
set
{
_FilePath = value;
}
}
但是当用户点击控件上的一个按钮进行一些操作时,FilePath的值在UserControl的代码中访问时是
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Some\\Path\\In\\The\\WebApp\\"
不是预期的
System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "Some\\Path\\In\\The\\WebApp\\" + AccountYearPeriodOptionsGroupRandomTestManager.SelectedAccountValue
(基本上缺少字符串中的AccountYearPeriodOptionsGroupRandomTestManager.SelectedAccountValue)。
奇怪的是,这些属性实际上会执行set 操作。其中一个肯定会执行该操作,但随后会立即失去其价值。 ModalTitle 属性将正确设置 UX,但之后访问该属性的值无法返回屏幕上显示的内容。
例如,以下设置访问器将正确设置屏幕上的TestManagerEditor_label 值,但无法设置_ModalTitle 的值:
用户控制代码:
/// <summary>
/// The text to display in the title of the Modal Dialog Box.
/// </summary>
private string _ModalTitle = "Test Manager";
/// <summary>
/// Gets or sets the text to display in the title of the Modal Dialog Box.
/// </summary>
public string ModalTitle
{
get
{
return _ModalTitle;
}
set
{
_ModalTitle = value;
TestManagerEditor_label.InnerText = value + " ";
TestManagerEditor_label.InnerHtml = TestManagerEditor_label.InnerHtml + "<span class=\"fa fa-pencil\"></span>";
}
}
有谁知道这里发生了什么以及为什么我的控件无法访问或保存其父页面设置的属性值?
【问题讨论】:
-
用户控件有自己的生命周期。尝试在渲染事件或数据绑定后读取/写入您的属性——您可能会看到不同的结果。 msdn.microsoft.com/en-us/library/ms178472.aspx
-
我会试试的,但我仍然很困惑为什么这些属性对页面“可见”而不是控件本身。
-
我想是因为那个时候,Eval 还没有运行。它可能在 Init() msdn.microsoft.com/en-us/library/… 之前运行
-
@KevinRaffay,我尝试遍历控件的 PreRender 事件和 Render 事件的 GridView 行,但没有成功。但是,在控件的 Load 事件上设置属性时,我是成功的。不知道为什么会这样,但是通过控件的属性设置属性值不起作用。奇怪的。谢谢您的帮助!它现在似乎可以工作了!
标签: c# asp.net properties user-controls asp-usercontrols