如果asp.net C#(SEARCH)中不存在记录,则显示弹出消息 [英] Display popup message if records not exists in asp.net C# (SEARCH)

查看:87
本文介绍了如果asp.net C#(SEARCH)中不存在记录,则显示弹出消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家,



我正在使用asp.net C#进行搜索,工作正常。

但面临问题其他不存在的记录。



如果用户在搜索文本框中输入其他记录,这些记录在数据库中不存在。

it应该显示一条弹出消息(抱歉找不到记录)。



这是我的搜索按钮代码:

Hello experts,

am working with search in asp.net C#, it is working fine.
but am facing problem with other records which are not existing.

if the user enter other records in Search Textbox,, which are not exisitng in database.
it should display a popup message as (Sorry Record not found).

This is my Search button code:

protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
    {
con.Open();
SqlCommand cmd = new SqlCommand("Select FirstName,LastName,EmpNo,ManagerName,Dept,Location,MobileNo,Email from Employees where FirstName='" + TxtSearch.Text + "' or EmpNo='" + TxtSearch.Text + "' ", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        GVRecords.DataSource = ds;
        GVRecords.DataBind();
        con.Close();
}



请帮帮我..



谢谢。


Please help me..

thanks.

推荐答案

已经作为评论共享:为什么弹出?多刺激了!为什么不是简单的标签文本?



如果您仍想显示弹出窗口,您可以使用XMLHttpRequest获取计数,如果count为零则显示消息。

或使用以下方法从服务器端注入JavaScript代码:

MSDN:RegisterClientScriptBlock方法 [ ^ ]

MSDN:RegisterStartupScript方法 [ ^ ]



尝试类似:

Already shared as a comment: Why popup? Thats irritating! Why not simple label text?

If you still want to show popup, you can either get the count using XMLHttpRequest and show message if count is zero.
Or Inject JavaScript code from server side using:
MSDN: RegisterClientScriptBlock Method[^]
MSDN: RegisterStartupScript Method[^]

Try something like:
Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "addScript", "alert('No records found')", true);






我愿意uggest在数据网格上方显示一个标签,显示搜索到的记录数。可以在验证搜索输入或提交表单时使用弹出窗口。

但如果没有记录,如果你想显示一个弹出窗口,你可以使用AjaxControlToolkit。



http://ajaxcontroltoolkit.codeplex.com/ 下载工具包[ ^ ]



添加引用工具包dll到你的ASP.Net项目和你的页面(就在@Page属性下面)

Hi,

I would suggest to show a label just above the data grid displaying the number of records searched. Popups can be used while validating the input for search or submit forms.
But if at all you want to display a popup if there are no records, you can use AjaxControlToolkit.

Download the tool kit from http://ajaxcontroltoolkit.codeplex.com/[^]

Add a reference of the tool kit dll to your ASP.Net project and to your page as (just below the @Page attribute)
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPages/MyTIO.Master" AutoEventWireup="true" CodeBehind="example.aspx.cs" Inherits="example.example.example" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>





添加一个asp:Panel(最初不显示)消息如下:





Add an asp:Panel (not displayed initially) for the message as below:

<asp:panel runat="server" id="pnlMessage" style="display:none" xmlns:asp="#unknown">
</asp:panel>





根据需要在CSS中格式化面板。



然后,在搜索按钮下方添加一个虚拟按钮,如下所示:





Format the panel in the CSS as you want.

Then, just below the search button, add a dummy button as follows:

<asp:button runat="server" id="btnSearchRecords" text="Search Records" onclick="btnSearchRecords_Click" xmlns:asp="#unknown" />
<asp:button runat="server" id="btnDummySearchRecords" style="display:none;" xmlns:asp="#unknown" />





然后按如下方式添加ajax控件工具包模式弹出扩展器:





Then add the ajax control tool kit modal popup extender as follows:

<asp:Button id="btnSearchRecords" Text="Search Records" onClick="btnSearchRecords_Click" />
<asp:Button id="btnDummySearchRecords" style="display:none;" />
<cc1:modalpopupextender id="mpeMessage" targetcontrolid="btnDummySearchRecords" backgroundcssclass="MessageBackground" xmlns:cc1="#unknown">
                    PopupControlID="pnlMessage"
                    OkControlID="btnClose" 
                    DropShadow="true
</cc1:modalpopupextender>





在c#中的按钮点击事件中,代码看起来很像如下:





In the button click event in c#, the code looks like as follows:

protected void btnSearchRecords_Click(object sender, EventArgs e)
{
    //Code to query the database and get the results in the datatable
    DataTable searchedRecords = new DataTable();
    searchedRecords = QueryDatabaseAndGetResults();
    
    //Binding the datatable to your datagrid and showing the popup if required
    this.gvResults.DataSource = searchedRecords;
    this.gvResults.DataBind();

    if(searchRecords.Rows.Count == 0)
    {
       this.mpeMessage.show();
    }
}







我希望这可以帮到你解决问题。随意拍出任何必要的东西。



快乐编码.. !!



干杯,

Nayan




I hope this will help you with your question. Feel free to shoot out anything required.

Happy coding..!!

Cheers,
Nayan


Hey Ranjith,



标签解决方案非常简单。

格式化你的页面如下:( CSS确实依赖你的外观和感觉)



Hey Ranjith,

Label solution will be very simple.
Format your page as below: (CSS does count on your look and feel)

<asp:label runat="server" id="lblTotalRecords" text="Total Records: 0" xmlns:asp="#unknown"></asp:label>
<asp:gridview runat="server" id="gvRecords" ...="" xmlns:asp="#unknown"></asp:gridview>





在你的代码背后:





In your code behind:

protected void SearchRecords()
{
    //code for connecting to the database and getting the results into DataTable
    //Assuming your are getting the results in the datatable named: dtResults

    this.gvRecords.DataSource = dtResults;
    this.gvRecords.DataBind();

    this.lblTotalRecords.Text = "Total Records: " + dtResults.Rows.Count.ToString();
}





这应该用于显示标签内的总记录

I希望这可以解决你的问题。

如果有帮助你,请不要忘记给答案。



快乐编码.. !!



问候,

Nayan



This should be working as to show the total records within a label
I hope this should resolve your question.
Don't forget to mark the answer if it helped you.

Happy coding..!!

Regards,
Nayan


这篇关于如果asp.net C#(SEARCH)中不存在记录,则显示弹出消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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