简体   繁体   English

ASP.NET WebForms确认

[英]ASP.NET WebForms Confirm

I'm new to web programming with .NET. 我是使用.NET进行Web编程的新手。

I am developing a web page with webforms, and I want at a certain moment to programmatically show a modal window, for the user to accept or cancel, according to a question. 我正在开发一个带有Web表单的网页,并且我想在某个时候以编程方式显示一个模式窗口,以供用户接受或取消。 Exactly what does the "confirm" function of JavaScript. JavaScript的“确认”功能究竟是什么。

I tried to get it calling a JavaScript function: 我试图让它调用JavaScript函数:

Page.ClientScript.RegisterStartupScript (this.GetType (), "CallMyFunction", "MyFunction()", true);

But I need to do it without reloading the page, and I also need to control if the user has accepted or canceled and I do not know how to do it. 但是我需要在不重新加载页面的情况下执行此操作,并且我还需要控制用户是否已接受或取消该操作,并且我不知道该怎么做。

I've also tried getting it using the ModExPopupExtender control from DevExpress. 我还尝试使用DevExpress的ModExPopupExtender控件来获取它。

Can someone tell me a simple way to get what I want? 有人可以告诉我一种获取我想要的东西的简单方法吗?

I can not understand how something so usual in web programming, and that PHP + javascript would not pose any problem can be so complicated. 我不明白如何在Web编程中如此常见,而PHP + javascript不会造成任何问题却是如此复杂。

All start in a one-button event on the code behind: 所有操作均以背后的代码的一键式事件开始:

  protected void btn1_Click(object sender, EventArgs e)
        {
           //I make a series of checks

           //If certain conditions I want to show the confirm

           //According to the user has chosen ok or cancel will perform a certain action
         }

Onclientclick does not help me because before launching the "confirm" I have to do some checks on the server side. Onclientclick对我没有帮助,因为在启动“确认”之前,我必须在服务器端进行一些检查。 Thank you very much. 非常感谢你。

You can use OnClientClick which is a property on most web controls. 您可以使用OnClientClick ,这是大多数Web控件上的属性。

I like to just bring up a simple confirm() dialog which executes the server code if the user clicks OK and does nothing if the user cancels the action: 我只想调出一个简单的confirm()对话框,如果用户单击“确定”,则执行服务器代码,如果用户取消操作,则不执行任何操作:

<asp:Button runat="server" ID="btnSave" Click="btnSave_Click" Text="Save"
    OnClientClick="return confirm('Are you sure you want to do this thing?');"  />

You can do other things with it as well, but the key thing to remember is that anything you do in OnClientClick will happen before the page gets posted back to the server. 您也可以使用它进行其他操作,但是要记住的关键是,在OnClientClick执行的任何操作都将页面重新发布到服务器之前发生。

This is also perfectly valid: 这也是完全有效的:

<asp:Button runat="server" ID="btnSave"
    OnClientClick="showModalConfirm('some message goes here');" ... />

<script>
    function showModalConfirm(msg)
    {
        $(".modal .message").innerHtml(msg);
        $(".modal").Show();
    }
</script>

You can set the action that OnClientClick should perform in your codebehind in exactly the same way: 您可以以完全相同的方式设置OnClientClick应该在您的代码背后执行的操作:

protected void Page_Load(object sender, EventArgs e)
{
    btnSave.OnClientClick = "return confirm('Are you sure you want to do this thing?');";
}

You can use below code in c# to call javascript function. 您可以在c#中使用以下代码来调用javascript函数。 Below code will execute afterpostback() javascript function: 下面的代码将执行afterpostback()JavaScript函数:

ClientScript.RegisterStartupScript(GetType(), Javascript, "javascript:afterpostback();", true);

And you can write code in javascript function to display any div or popup: 您可以在javascript函数中编写代码以显示任何div或弹出窗口:

<script language="javascript" type="text/javascript">
 function afterpostback() {

            //Here you can write javascript to display div/modal
    }
</script>

One way I've handled this previously was to have 2 buttons on the page. 我以前处理此问题的一种方法是在页面上有2个按钮。 The first would be initially visible and labeled "Submit". 第一个将最初可见,并标记为“提交”。 The second would be initially hidden and labeled "Confirm". 第二个将最初被隐藏并标记为“确认”。 The "Submit" button would postback upon click and perform your server side checks/validation. 点击后,“提交”按钮将回传并执行服务器端检查/验证。 If those checks failed, an appropriate error message would be displayed. 如果这些检查失败,将显示相应的错误消息。 If those checks passed, an appropriate "Please confirm your submission"-type message would be displayed, the "Submit" button would become hidden, and the second "Confirm" button would become visible. 如果通过了这些检查,将显示适当的“请确认您的提交”类型的消息,“提交”按钮将变为隐藏,第二个“确认”按钮将变为可见。 When that Confirm button was clicked, it would postback again and fully submit. 单击该“确认”按钮后,它将再次回传并完全提交。

EDIT: I forgot to mention, there's a bit more to this that occurred to me after I initially posted. 编辑:我忘了提一下,在我最初发布之后,我想到的还有更多东西。 You'll have to protect the fields from being edited in the event the server-side verification is successful as you obviously don't want the user changing values and then clicking the Confirm button. 如果服务器端验证成功,则必须保护字段不被编辑,因为您显然不希望用户更改值,然后单击“确认”按钮。 That means disabling all the input controls - which could be a pain if you have a lot. 这意味着禁用所有输入控件-如果您操作太多,可能会很麻烦。 You also have to give them a way to (intentionally) Edit in case the server side verification passes, you display the Confirmation, and they change their minds - so basically you'd need a third "Cancel/Edit"-type button that would put the form back in edit mode and show your initial Submit button. 您还必须给他们提供一种方法(有意地)进行编辑,以防服务器端验证通过,显示确认,并且他们改变主意-因此,基本上,您需要第三个“取消/编辑”类型的按钮,将表单放回编辑模式,并显示您的初始“提交”按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM