简体   繁体   English

ASP.Net-需要单击代码后面的按钮,这样我才能弹出错误消息

[英]ASP.Net - need to click a button from the code-behind so I can pop an error message

Inherited an asp.net 3.5 C# site. 继承了一个asp.net 3.5 C#网站。 Main page has a control in it that has a datagrid-if the user fails to put a file name and hits 'update', it's wrong-they have to supply a file name. 主页面中的控件具有一个数据网格-如果用户未能输入文件名并单击“更新”,那是错误的-他们必须提供文件名。 They also have some custom business logic that I nicely have in the code behind. 他们还具有一些自定义业务逻辑,这些逻辑我很好地包含在后面的代码中。 Any of these could mean the user needs to see an error at which point I cancel out of the SqlDataSource update, thus, reverting the grid back to its original value, fine...however, if I put an error message up in a text box(already did that), now, the user is staring at an error msg...I'd rather pop up a warning / error, and then it's all fixed. 这些中的任何一个都可能意味着用户需要在我从SqlDataSource更新中取消的那一点上看到一个错误,因此,将网格还原回其原始值,很好...但是,如果我在文本中输入错误消息框(已经做到了),现在,用户盯着一个错误消息...我宁愿弹出一个警告/错误,然后一切都已解决。

SO.....I have the error in a hiddenfield...now, how can I force a click of a hidden button so I can alert the client? 所以.....我在隐藏字段中有错误...现在,我该如何强制单击隐藏按钮以提醒客户端?

The following is the code which validates the user input at code behind, and prompt a message if invalid. 以下是验证后面的代码中的用户输入的代码,如果无效,则提示消息。

// ASPX page
<asp:TextBox runat="server" ID="FileNameTextBox"></asp:TextBox>
<asp:Button runat="server" ID="SubmitButton" Text="Submit" OnClick="SubmitButton_Click" />

// Code behind
protected void SubmitButton_Click(object sender, EventArgs e)
{
    if (string.IsNullOrEmpty(FileNameTextBox.Text))
    {
        string script = string.Format("alert('File name is required.')");

        ScriptManager.RegisterStartupScript(Page, typeof(Page), "Validation" + UniqueID, script, true);
    }
}

在此处输入图片说明

How to trigger it from code behind: see here 如何从后面的代码中触发它: 请参见此处

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

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