是否有任何ASP.NET原生的方式做了一个"成功消息"? [英] Is there any native way in ASP.NET to do a "success message"?

查看:93
本文介绍了是否有任何ASP.NET原生的方式做了一个"成功消息"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有类似的ASP.NET ASP:DetailsView控件显示,并在数据库中编辑单个记录

Say you have something like an ASP.NET ASP:DetailsView to show and edit a single record in a database.

这是简单的记录错误的情况下......要添加验证和验证摘要。当您更新表单验证失败,这自然使得噪音:它显示的验证消息及/或验证摘要。没有哪一个code后面是必需的。

It's simple to record the error cases... you add validation and a validation summary. When your update form fails validation it naturally makes noise: it shows the validation message and/or the validation summary. Not a single code behind is required.

但是,你通过验证,它使您的更新完全安静。有没有感觉发生了什么情况,而且似乎没有任何默认设置,使成功的消息,而code-屁股。

But then, you pass validation, and it makes your update completely silently. There's no sense that anything happened, and there doesn't seem to be any default settings to make a success message without code-behinds.

但是,即使code-屁股混淆。什么情况下应该显示成功消息? onItemUpdate,对不对?好吧,但后来我们说,你再拍变化,并得到验证错误?您的成功消息保持。我没能找到,如果有一个验证错误,将可靠地关闭现有的成功消息的事件。

But, even code-behinds are confusing. What event should show the success message? onItemUpdate, right? Fine, but then let's say you make another change and get a validation error? Your success message stays. I wasn't able to find an event that would reliably turn off an existing success message if there were a validation error.

这应该是Web开发101!为什么这么难?

This should be web development 101! Why is it so hard?

编辑:

有人使用ItemCommand事件......我想这和许多其他活动的建议,但成功的消息就不会消失。下面是一些code。

Someone suggested using the ItemCommand event... I tried this and many other events, but that success message just won't disappear. Here's some code.

我在ASP.NET消息

My message in ASP.NET

<label id="successMessage" class="successMessage" runat="server"></label>

和我的数据视图标签(简体):

And my DataView tag (simplified):

    <asp:DetailsView 
        Id="EditClient"
        DataKeyNames="LicenseID" 
    DataSourceID="MySource"
    runat="server" 
        OnItemUpdated="SuccessfulClientUpdate"
        OnItemCommand="ClearMessages">

和我的code-背后:

And, my code-behind:

protected void SuccessfulClientUpdate(object sender, DetailsViewUpdatedEventArgs e)
{
    successMessage.InnerText = string.Format("Your changes were saved.");
    successMessage.Visible = true;
}

protected void ClearMessages(object sender, DetailsViewCommandEventArgs e)
{
    successMessage.InnerText = string.Empty;
    successMessage.Visible = false;
}

在我做一个成功的更新,但是,似乎没有任何使该消息消失,甚至没有失败的验证。

Once I do a successful update, however, nothing seems to make that message disappear, not even failed validation.

2日编辑:

只是要清楚,我也尝试把ClearMessages code在Page_Load中。然而,似乎没有任何做出successMessage标签消失时,我打更新验证错误第二次。任何人都可以提出任何其他的故障排除技巧?

Just want to be clear that I did try putting the ClearMessages code in Page_Load. However, nothing seems to make that successMessage label disappear when I hit update a 2nd time WITH a validation error. Can anyone suggest any other troubleshooting tips?

推荐答案

据我所知,没有这样做的没有原生方式。你可以夸夸其谈一番,也许微软会听到它。)

As far as I know, there is no native way of doing this. You may rant about it, maybe Microsoft will hear it :).

重置上的Page_Load成功消息,或任何在code-的背后,是行不通的。这是因为,ASP.NET验证通常完成客户端和服务器端。这意味着你把每个页面上的验证控件,ASP.NET产生一些客户端JavaScript,做了验证,并呈现在客户端上的错误,要回服务器。所以,你不得不拥有两个成功的消息,并在同一时间的错误消息。

Resetting the "success message" on Page_Load, or wherever in your code-behind, won't work. This is because ASP.NET validation is usually done both client and server-side. This means for every validation control you put on the page, ASP.NET generates some client-side Javascript that does the validation and renders the error on the client, without going back to the server. So you're stuck with both the success message and the error message at the same time.

你可以做些什么:


  • 将一个&LT; D​​IV&GT; 页面上控制,即会显示成功消息(如已被别人建议以上)。当你更新的东西(在服务器端code),显示控制,并设置一个有意义的成功!消息文本。

  • 注册自定义JavaScript函数,将查找在&LT; D​​IV&GT; 键,将其隐藏在每个页面上提交。请注意,函数需要自动生成客户端脚本,并验证之前调用。

  • place a <div> control on your page, that would show up the success message (as already suggested by others above). Whenever you update something (in server-side code), show up the control and set a meaningful "Successful!" message text.
  • register a custom Javascript function that would lookup the <div> and hide it on every page submit. Be aware that the function needs to be called before the autogenerated client script that does the validation.

如果你看一下ASP.NET页面(上面有验证)的客户端源,这里你可以找到:

If you look at the client source of an ASP.NET page (with validators on it), here's what you can find:

&LT;表格名称=aspnetForm方法=邮报行动=MyPage.aspx的onsubmit =JavaScript的:返回WebForm_OnSubmit(); ID =aspnetForm&GT;

该WebForm_OnSubmit由ASP.NET生成和调用确实验证的JavaScript。示例:

The WebForm_OnSubmit is generated by ASP.NET and calls the javascript that does the validation. Sample:

function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
	return false;
return true;
}

要注册您的自定义code隐藏了成功的消息,你应该把(在code-后面)沿着这些路线的内容:

To register your custom code that hides the success message, you should place (in your code-behind) something along these lines:

if (!Page.ClientScript.IsOnSubmitStatementRegistered("ClearMessage"))
{
	string script = @"document.getElementById('" + 
		yourDivControl.ClientID + "').style.display='none'";
	Page.ClientScript.RegisterOnSubmitStatement(Page.GetType(), "ClearMessage", script);
}

这将打开你的网页的自动生成WebForm_OnSubmit到以下内容:

This will turn your page's autogenerated WebForm_OnSubmit into the following:

function WebForm_OnSubmit() {
	document.getElementById('ctl00_yourDivControl').style.display='none';
	if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false)
		return false;
	return true;
}

效果:
在每一个回发(例如,当ItemCommand被触发时,或点击一些保存按钮,或其他任何东西),你会出现在成功的消息div控件。在接下来的回传,刚刚提交页面到服务器之前,此消息被清除。当然,如果这回发还触发一个成功,再次被$ C $在服务器上的c-后面示出的消息。等等等等。

The effect: On every postback (e.g. when ItemCommand is triggered, or when some Save button is clicked, or anything else), you will show up the div control with the "success" message. On the next postback, just before submitting the page to the server, this message is cleared. Of course, if this postback also triggers a "success", the message is shown again by the code-behind on the server. And so on and so forth.

我希望以上是很有帮助的。这不是全面的解决方案,但它提供了足够的提示,你指出正确的方向。

I hope the above is helpful. It's not the full-blown solution, but it gives sufficient hints to point you in the right direction.

这篇关于是否有任何ASP.NET原生的方式做了一个&QUOT;成功消息&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆