另一个 UserControl 模板内的 UserControl 在回发时丢失值
这可能是真正错误的做法,所以如果是的话请告诉我!我有一个名为 ucDropDownList.ascx 的用户控件。它基本上是一个下拉菜单,旁边还有一个按钮。单击该按钮时,将出现一个 jquery 对话框。对话框是问题所在。由于这个用户控件用在我想要的许多地方(在 OOP 的原则内),以允许我们根据它所在的下拉列表以不同的形式加载......所以我选择了模板用户控件。其背后的代码如下:
public partial class ucDropDownList : System.Web.UI.UserControl
{
private ITemplate m_RecordForm = null;
[TemplateContainer(typeof(cPopUpContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate RecordForm
{
get { return m_RecordForm; }
set { m_RecordForm = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
void Page_Init()
{
// Load in the Template control (we use this for making the different types of form!
if (RecordForm != null)
{
cPopUpContainer container = new cPopUpContainer();
m_RecordForm.InstantiateIn(container);
phNewRecordForm.Controls.Add(container);
}
}
public event EventHandler SaveClicked;
protected void SaveButton_Clicked(object sender, EventArgs e)
{
if (SaveClicked != null)
SaveClicked(this, e);
}
}
public class cPopUpContainer: Control, INamingContainer
{
internal cPopUpContainer()
{
}
}
然后我(将)有几个用户控件,它们是我将拥有这些用户控件的每种类型的简单形式...(给出的示例是数据库中的状态表)。那里没有什么令人兴奋的,只是所有的控件都启用了 viewState。 (几乎与标准一样)。
我显示这些的页面有一些像这样的标记...
<uc3:ucDropDownList ID="comboStatus" runat="server" OnSaveClicked="comboStatus_Saved">
<RecordForm>
<uc4:ucEdStatus ID="ucEdStatus1" runat="server" ViewStateMode="Enabled" />
</RecordForm>
</uc3:ucDropDownList>
以及像这样的幕后...
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
ucEdStatus statForm = comboStatus.Controls[3].Controls[0].FindControl("ucEdStatus1") as ucEdStatus;
statForm.InitializeControl(false);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void comboStatus_Saved(object sender, EventArgs e)
{
ucEdStatus statForm = comboStatus.Controls[3].Controls[0].FindControl("ucEdStatus1") as ucEdStatus;
statForm.SaveRecord(eSaveUCEdType.Insert);
}
当 SaveRecord 运行时(此方法是一个简单的插入到数据库中的操作,它基于各种控件的值) ucEdStatus1 控件)ucEdStatus 上的控件都是标记中设置的默认值,并且没有维护用户输入的内容(因此保存仅保存了错误的数据位)。
我愿意接受建议和批评。如果您需要更多信息,我可以给您,我只是需要帮助来理解为什么会发生这种情况。
编辑 以防万一你们想知道这是 ucEdStatus 控件的类。它可能会回答为什么我仍然摸不着头脑,或者可能会显示我有多么愚蠢。
long m_RecordID
{
get { return (ViewState["recordid"] != null ? Convert.ToInt64(ViewState["recordid"]) : -1); }
set { ViewState["recordid"] = value; }
}
bool m_isSupport
{
get { return (ViewState["isSupport"] != null ? Convert.ToBoolean(ViewState["isSupport"]) : false); }
set { ViewState["isSupport"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
//--------------------------------------------------------------------------------
public void InitializeControl(bool aIsSupport)
{
//Setup the issupport variable
m_isSupport = aIsSupport;
//Add the importance to the mixup...
Array importanceTexts = System.Enum.GetNames(typeof(eStatusImportance));
for (int i = 0; i <= importanceTexts.Length - 1; i++)
{
ListItem item = new ListItem(importanceTexts.GetValue(i).ToString(), i.ToString());
comboImportance.Items.Add(item);
}
}
//--------------------------------------------------------------------------------
public long SaveRecord(eSaveUCEdType aSaveType)
{
using (cDBConnection con = new cDBConnection(this.Page, true))
{
cStatus stat = new cStatus(con.Con, m_RecordID);
if (m_RecordID == -1)
stat.Id = cGlobalDB.NewKey();
stat.Importance = Convert.ToInt32(comboImportance.SelectedValue);
stat.Name = txtName.Text;
stat.Issupport = m_isSupport;
stat.Color = edColor.Color;
stat.SaveChanges((aSaveType == eSaveUCEdType.Insert ? eUpdateType.insert : eUpdateType.update));
return (long)stat.Id;
}
}
This may be the really wrong way of doing this and so if it is please let me know!! I have a user control called ucDropDownList.ascx. It basically is a drop down with a button besides it. When the button is clicked a jquery dialog appears. The dialog is the problem. As this user control is used in a number of places i wanted (within the principles of OOP) to allow us to load in a different form depending on which drop down it was...So i went for Template User Control. The code behind for this is as follows:
public partial class ucDropDownList : System.Web.UI.UserControl
{
private ITemplate m_RecordForm = null;
[TemplateContainer(typeof(cPopUpContainer))]
[PersistenceMode(PersistenceMode.InnerProperty)]
public ITemplate RecordForm
{
get { return m_RecordForm; }
set { m_RecordForm = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
void Page_Init()
{
// Load in the Template control (we use this for making the different types of form!
if (RecordForm != null)
{
cPopUpContainer container = new cPopUpContainer();
m_RecordForm.InstantiateIn(container);
phNewRecordForm.Controls.Add(container);
}
}
public event EventHandler SaveClicked;
protected void SaveButton_Clicked(object sender, EventArgs e)
{
if (SaveClicked != null)
SaveClicked(this, e);
}
}
public class cPopUpContainer: Control, INamingContainer
{
internal cPopUpContainer()
{
}
}
Then i (will) have several user controls that are simple forms for each of the types that i will have these user controls for...(example given is a status table in the database). There is nothing exciting there, just that all of the controls have viewState Enabled. (pretty much as standard as it comes).
The page that i show these on has some markup like so...
<uc3:ucDropDownList ID="comboStatus" runat="server" OnSaveClicked="comboStatus_Saved">
<RecordForm>
<uc4:ucEdStatus ID="ucEdStatus1" runat="server" ViewStateMode="Enabled" />
</RecordForm>
</uc3:ucDropDownList>
and a behind the scenes like so...
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack)
{
ucEdStatus statForm = comboStatus.Controls[3].Controls[0].FindControl("ucEdStatus1") as ucEdStatus;
statForm.InitializeControl(false);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void comboStatus_Saved(object sender, EventArgs e)
{
ucEdStatus statForm = comboStatus.Controls[3].Controls[0].FindControl("ucEdStatus1") as ucEdStatus;
statForm.SaveRecord(eSaveUCEdType.Insert);
}
When the SaveRecord runs(this method is a simple insert into the database which is based on the values of the various controls on the ucEdStatus1 control) the controls on the ucEdStatus are all the default as set in the markup and non have maintained what the user put in (therefore the save just saves a wrong bit of data).
I am open to suggestions and criticism. If you need more information i can give it to you, i just need help in understanding why this is happening.
EDIT
Just in case you guys were wondering this is the class for the ucEdStatus control. It may answer why i am still scratching my head or may show how stupid i am.
long m_RecordID
{
get { return (ViewState["recordid"] != null ? Convert.ToInt64(ViewState["recordid"]) : -1); }
set { ViewState["recordid"] = value; }
}
bool m_isSupport
{
get { return (ViewState["isSupport"] != null ? Convert.ToBoolean(ViewState["isSupport"]) : false); }
set { ViewState["isSupport"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
}
//--------------------------------------------------------------------------------
public void InitializeControl(bool aIsSupport)
{
//Setup the issupport variable
m_isSupport = aIsSupport;
//Add the importance to the mixup...
Array importanceTexts = System.Enum.GetNames(typeof(eStatusImportance));
for (int i = 0; i <= importanceTexts.Length - 1; i++)
{
ListItem item = new ListItem(importanceTexts.GetValue(i).ToString(), i.ToString());
comboImportance.Items.Add(item);
}
}
//--------------------------------------------------------------------------------
public long SaveRecord(eSaveUCEdType aSaveType)
{
using (cDBConnection con = new cDBConnection(this.Page, true))
{
cStatus stat = new cStatus(con.Con, m_RecordID);
if (m_RecordID == -1)
stat.Id = cGlobalDB.NewKey();
stat.Importance = Convert.ToInt32(comboImportance.SelectedValue);
stat.Name = txtName.Text;
stat.Issupport = m_isSupport;
stat.Color = edColor.Color;
stat.SaveChanges((aSaveType == eSaveUCEdType.Insert ? eUpdateType.insert : eUpdateType.update));
return (long)stat.Id;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
每次尝试在 page_init 中初始化该控件;换句话说,删除
!IsPostback
检查。所有控件都必须在页面初始化期间初始化,然后才能应用其视图状态信息。这与在 aspx/ascx 文件中定义 asp.net 控件完全相同(这些文件中的任何内容都会经历相同的过程,由 asp.net 自动为您完成)。Try initializing that control in page_init every time; in other words, remove the
!IsPostback
check. All controls have to be initialized during page-init before their viewstate information will be applied. This is exactly equivalent as defining an asp.net control within aspx/ascx files (whatever is in those files goes through the same process, automatically for you done by asp.net).所以我环顾四周后找到了答案。
这个网站确实很有帮助。问题是 jquery 模式弹出窗口(对话框)在回发时丢失了自身。一根线修复。多么令人沮丧啊!
So i found the answer after a little looking around.
This site really helped. The problem was the jquery modal pop up (dialog) was losing itself on post back. One line fix. How frustrating!