简体   繁体   English

使用代码后面弹出aspx确认消息

[英]use code behind to pop aspx confirmation message

I need to return a confirmation message to OnClientClick event. 我需要向OnClientClick事件返回一条确认消息。 The problem is I have to get the message from stored procedure, and I don't seem to call my function right. 问题是我必须从存储过程中获取消息,而我似乎没有调用我的函数。

 <asp:ImageButton 
OnClientClick="return confirm(<%# GetConfirmExportMessage()%>);"            
OnClick="btnExportRed_Click" 
runat="server" 
ImageUrl="~/Images/excel_red.gif" 
ID="btnExportRed" 
AlternateText="Export all records!"/>

My code behind: 我的代码背后:

public string GetConfirmExportMessage()
    {
        string xmlFilterForExport = Parameters.ToString().Split('+')[4].Trim();
        string alertMessage = companiesSearcher.GetAlertMessage(xmlFilterForExport, 1, null, null, IdSegment, 1, Convert.ToInt32(RoDB2004.BLL.clsUsers.UserID));

        return alertMessage;
    }

Try using equal sign instead of pound (and don't forget your single quotes). 尝试使用等号而不是磅(并且不要忘记单引号)。

OnClientClick="return confirm('<%= GetConfirmExportMessage()%>');"

However, if you're ImageButton is located inside a DataGrid or GridView, the server-side code will not be evaluated and will yeield an alert saying <%= GetConfirmExportMessage()%> instead of the real message. 但是,如果您的ImageButton位于DataGrid或GridView中,则不会评估服务器端代码,并且会发出一条警告,说明<%= GetConfirmExportMessage()%>而不是真实消息。

To get around this problem (and to increase performance, throughput, etc.), output your message once to a variable, then alert the contents of the variable. 要解决此问题(并提高性能,吞吐量等),请将消息输出一次到变量,然后提醒变量的内容。

<script language="JavaScript">
    var myMessage = '<%= GetConfirmExportMessage()%>';
</script>

Later, in the GridView... 后来,在GridView中......

OnClientClick="return confirm(myMessage);"

You save throughput by repeating a small variable name like "myMessage" and avoid repeating some big message. 通过重复一个小的变量名称(如“myMessage”)来节省吞吐量,并避免重复一些重要的消息。 Also note, that the method GetConfirmExportMessage isn't called dozens of times increasing performance. 还要注意, GetConfirmExportMessage方法没有被调用数十倍的性能提升。

If your message is specific to data within a current row and not static like "Are you sure?", then I suggest you perform this operation inside the GridView's RowDataBound event. 如果您的消息特定于当前行中的数据而不是静态的,例如“您确定吗?”,那么我建议您在GridView的RowDataBound事件中执行此操作。 You'll have full access to the ImageButton and to the data the current row is binding to, which makes it very easy to setup server-side. 您可以完全访问ImageButton以及当前行绑定的数据,这使得设置服务器端变得非常容易。 Check out the FindControl() method, or if you know the exact location, just reference it and unbox the object. 查看FindControl()方法,或者如果您知道确切位置,只需引用它并取消装箱对象。

protected void gvMyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        ImageButton ibtn = (ImageButton)e.Row.FindControl("btnExportRed");
        if(ibtn != null)
        {
            ibtn.ClientClick = "return confirm('" + GetConfirmExportMessage() + "');";
        }
    }
}

As an alternative solution, maybe you should look into WebMethods, AJAX and jQuery. 作为替代解决方案,也许你应该研究WebMethods,AJAX和jQuery。 This solution is probably the better one because the data is not sent to the client and only retrieved when necessary. 此解决方案可能更好,因为数据不会发送到客户端,只在必要时才会检索。

You probably want 你可能想要

OnClientClick="return confirm('<%= GetConfirmExportMessage()%>');"             

Note that this will be populated when the page is rendered, not when the button is clicked, so you may as well use 请注意,这将在呈现页面时填充,而不是在单击按钮时填充,因此您也可以使用

btnExportRed.OnClientClick = "javascript:return confirm('" + GetConfirmExportMessage() + "');" 

in your code behind. 在你的代码背后。

you need to wrap your confirm in ' 你需要把你的确认包裹起来'

<asp:ImageButton 
    OnClientClick="return confirm('<%# GetConfirmExportMessage()%>');"            
    OnClick="btnExportRed_Click" 
    runat="server" 
    ImageUrl="~/Images/excel_red.gif" 
    ID="btnExportRed" 
    AlternateText="Export all records!"/>

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

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