如何显示错误消息 [英] How to display error message

查看:53
本文介绍了如何显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前这是我的.cs文件中的代码;注意它确实'抛出新的NotImplementedException();',所以当从我的表单向数据库输入重复字段时,抛出此异常是很常见的。字段名称为'Awards_Title'



Currently this is the code in my .cs file; notice it does 'throw new NotImplementedException();', so it is ment to throw this exception when a duplicate field is being entered from my form into the database. The field name is 'Awards_Title'

public partial class NewPages_Create_New_Awards : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["NEW"] != null)
        {
            //Label1.Text += Session["NEW"].ToString();
            //Label2.Text = Request.QueryString.ToString(); - QueryString taking information from another page.
        }
        else
        {
            Response.Redirect("~/NewPages/Admin Pages/NewLogin.aspx");
        }

        SqlDataSource1.Inserted += new SqlDataSourceStatusEventHandler(SqlDataSource1_Inserted);
    }

    void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
    {
        throw new NotImplementedException();

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Form data saved.')", true);
        }
    }
}





这是我的aspx文件中的代码< br $>




This is the code that is currently in my aspx file

<asp:TemplateField HeaderText="Title *" SortExpression="Awards_Title">
    <EditItemTemplate><asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Awards_Title") %>'></asp:TextBox></EditItemTemplate>
    <InsertItemTemplate><asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Awards_Title") %>'></asp:TextBox></InsertItemTemplate>
     <ItemTemplate><asp:Label ID="Label1" runat="server" Text='<%# Bind("Awards_Title") %>'></asp:Label></ItemTemplate>
</asp:TemplateField>





所以我想要做的是抛出NotImplementedException()时..在Awards_Title文本框(TextBox1)旁边应该有一条消息,说'已经创建了奖项标题,请使用唯一名称'。



有人可以帮我这个这是一个非常紧急的事情!!



So what I am want to do is when NotImplementedException() is thrown .. there should be a message beside Awards_Title text box (TextBox1) saying 'Award Title has already been created please use a unique name'.

Can someone please help me with this it is a very urgent!!

推荐答案

你可以在代码中添加'try catch'块

You can add 'try catch' block to the code
try{
   throw new NotImplementedException();
}catch(Exception ex){
   lblErrorMessage.Text=ex.Message;
}



所以,你可以收到错误信息并将其打印在标签上。





更好的版本是在提交按钮之前检查重复。在文本框的丢失焦点(更改)事件上使用ajax调用。这需要一些时间,而且不会太复杂。


so, you can catch the error message and print it on a label.


Still the better version is to check the duplication before submit button. Using ajax call on the Textbox's lost focus (change) event. It will take some time and will be little complex.


这篇关于如何显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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