我有一个简单的用户控件来将数据插入到数据库中。我为用户控件创建了一个名为isInModifyMode的公共属性。如果在父窗体控件的click事件上单击编辑按钮为true,然后在父页面的页面加载事件上,将usercontrols页面载入的属性设置为true我检查了属性IsinModifymode并呈现与填充文本框中的值和所有其他控件从修改数据库模式的控制(即我更改按钮的Text属性更新,而不是保存)有条件地呈现用户控件
但代码不能正常工作。如果有任何机构可以提前帮助,请提前致电 。
我有一个简单的用户控件来将数据插入到数据库中。我为用户控件创建了一个名为isInModifyMode的公共属性。如果在父窗体控件的click事件上单击编辑按钮为true,然后在父页面的页面加载事件上,将usercontrols页面载入的属性设置为true我检查了属性IsinModifymode并呈现与填充文本框中的值和所有其他控件从修改数据库模式的控制(即我更改按钮的Text属性更新,而不是保存)有条件地呈现用户控件
但代码不能正常工作。如果有任何机构可以提前帮助,请提前致电 。
它仍然不是很清楚你想要做什么,究竟是什么不起作用。但我会在若干假设回答取刺:
原理很简单,有许多方法可以做到什么,我想你问。一种方法是基本上将事件绑定到按钮以设置控件的isInModifyMode属性。在控制本身中,让属性的setter调用一个更新显示的方法。这在下面的非常简陋和低于完美的例子所示:
Default.aspx的
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Src="WebUserControl.ascx" TagName="WebUserControl" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<uc1:WebUserControl ID="WebUserControl1" runat="server" isInModifyMode="false" />
<asp:Button ID="EditButton" runat="server" Text="Edit" />
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected override void OnInit(EventArgs e)
{
// Assign the handler for the edit button click event
EditButton.Click += new EventHandler(EditButton_Click);
base.OnInit(e);
}
/// <summary>
/// Edit button click handler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void EditButton_Click(object sender, EventArgs e)
{
WebUserControl1.isInModifyMode = true;
}
}
WebUserControl1 .ascx
个<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:PlaceHolder ID="ReadOnlyModePlaceholder" runat="server">
<p>Read Only Mode</p>
</asp:PlaceHolder>
<asp:PlaceHolder ID="EditModePlaceholder" runat="server" Visible="false">
<p>Edit Mode</p>
</asp:PlaceHolder>
WebUserControl1.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class WebUserControl : System.Web.UI.UserControl
{
/// <summary>
/// Changes the display mode by toggling the appropriate controls on or off
/// based on the isInModifyMode property
/// </summary>
private void SetDisplayMode()
{
if (!this.isInModifyMode)
{
// Render as not modify
ReadOnlyModePlaceholder.Visible = true;
EditModePlaceholder.Visible = false;
}
else
{
// Render as modify
ReadOnlyModePlaceholder.Visible = false;
EditModePlaceholder.Visible = true;
}
}
/// <summary>
/// Determines whether or not the control should be displayed in edit mode or not.
/// </summary>
public bool isInModifyMode
{
get
{
if (ViewState["isInModifyMode"] != null)
return (bool)ViewState["isInModifyMode"];
else
return false;
}
set
{
ViewState["isInModifyMode"] = value;
// Since we're possibly changing modes, call
// the SetDisplayMode() method to update the display
SetDisplayMode();
}
}
protected void Page_Load(object sender, EventArgs e)
{
// Set the display mode on the initial load
if (!IsPostBack)
SetDisplayMode();
}
}
感谢kiddailey如果worls andlet你千牛超重,我会尝试此解决方案 – Devjosh 2011-05-05 09:26:50
看一看在ASP.Net page life cycle和事件顺序。在ButtonClick前
页面加载和控制系统负载事件被触发。将渲染逻辑移至PreRender事件。
如果这没有帮助,添加一些代码,以便更好地理解这个问题的。
相信我会尽力谢谢 – Devjosh 2011-05-05 09:28:06
你能否提供一些代码片段。根据你的描述很难理解这个问题。你说,“...父窗体控件的点击事件...”我相信你的意思是在父窗体上的按钮上的点击事件,但我不是100%确定的。 – GunnerL3510 2011-05-05 07:00:47
谢谢GunnerL3510我所做的更改后,以使其更容易理解 – Devjosh 2011-05-05 08:35:24