我有一个 ASP.NET WebApplication 页面,其中 DetailsView 绑定到 SqlDataSource。
当我尝试通过自动生成的编辑或插入按钮更改模式时,会触发事件。
当我尝试通过 DetailsView.ChangeMode 方法调用(在自定义按钮单击内部)以编程方式更改模式时,不会触发 ModeChanging 和 ModeChanged 事件。
为什么它不起作用?
有aspx列表:
<form id="form1" runat="server">
<div>
<h1><asp:Label ID="Label" runat="server" Text="ReadOnly" /></h1>
<asp:DetailsView ID="DetailsView" runat="server"
DataSourceID="SqlDataSource"
EnableModelValidation="True"
AllowPaging="True"
AutoGenerateEditButton="True"
AutoGenerateInsertButton="True"
OnModeChanged="DetailsView_ModeChanged">
<FooterTemplate>
</FooterTemplate>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:PEMDConnectionString %>"
SelectCommand="SELECT * FROM [evd].[WaterTemplates]"
InsertCommand="INSERT INTO [evd].[WaterTemplates] (TemplateName,TemplateDescription) VALUES (@Name,@Description)" />
<asp:Button ID="InsertByChangeModeButton" runat="server"
OnClick="InsertByChangeModeButton_Click"
Text="Insert By ChangeMode" />
<asp:Button ID="EditByChangeModeButton" runat="server"
OnClick="EditByChangeModeButton_Click"
Text="Edit By ChangeMode" />
</div>
</form>
这是后面的代码:
namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
switch (DetailsView.CurrentMode)
{
case DetailsViewMode.Edit:
case DetailsViewMode.Insert:
EditByChangeModeButton.Visible = false;
InsertByChangeModeButton.Visible = false;
break;
case DetailsViewMode.ReadOnly:
InsertByChangeModeButton.Visible = true;
EditByChangeModeButton.Visible = true;
break;
default:
break;
}
}
protected void InsertByChangeModeButton_Click(object sender, EventArgs e)
{
DetailsView.ChangeMode(DetailsViewMode.Insert);
}
protected void EditByChangeModeButton_Click(object sender, EventArgs e)
{
DetailsView.ChangeMode(DetailsViewMode.Edit);
}
protected void DetailsView_ModeChanged(object sender, EventArgs e)
{
switch (DetailsView.CurrentMode)
{
case DetailsViewMode.Edit:
Label.Text = "Edit";
break;
case DetailsViewMode.Insert:
Label.Text = "Insert";
break;
case DetailsViewMode.ReadOnly:
Label.Text = "ReadOnly";
break;
default:
break;
}
}
}
}