在数据库中搜索名称时应该为文本框触发什么事件 [英] What event should fire for textbox while searching name in database

查看:80
本文介绍了在数据库中搜索名称时应该为文本框触发什么事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我的搜索文本框有一个表格。如果输入任何名称或registration_id,它应显示gridview与相关的详细信息...我得到的结果,但没有以正确的方式...我从text-box1.but进入text-box1_text-changed事件只有我想看到细节

//这是我的sql-query

Actually i am having a form with search text-box1. in that if enter any name or registration_id it should display gridview with related details...i am getting the result but not in a correct way...i taken text-box1_text-changed event after going from text-box1.but while entering only i want to see the details
//this was my sql-query

select * from student_details where (child_name like @Student_ID or student_id like @Child_Name) and School_Name=@School_Name

//这是我的.cs代码

//this was my .cs code

protected void txtSearch_TextChanged(object sender, EventArgs e)
       {
           gridviewCandidateDetails.Visible = true;
           SqlCommand cmdSearch = DBManager.DataAccess.command();
           cmdSearch.Parameters.Add(new SqlParameter("@option", SqlDbType.VarChar, 50));
           cmdSearch.Parameters["@option"].Value = "SearchByNameOrRegistrationID";

           cmdSearch.Parameters.Add(new SqlParameter("@School_Name", SqlDbType.NVarChar, 200));
           cmdSearch.Parameters["@School_Name"].Value = Session["SchoolName"].ToString();

           cmdSearch.Parameters.Add(new SqlParameter("@Student_ID", SqlDbType.VarChar, 50));
           cmdSearch.Parameters["@Student_ID"].Value = txtSearch.Text;

           cmdSearch.Parameters.Add(new SqlParameter("@Child_Name", SqlDbType.VarChar, 50));
           cmdSearch.Parameters["@Child_Name"].Value = txtSearch.Text;

           string sqlquery = "SchoolProc";
           DataSet dsPickSearchDetails = DBManager.DataAccess.getdata(sqlquery);
           if (dsPickSearchDetails.Tables[0].Rows.Count > 0)
           {
               gridviewCandidateDetails.DataSource = dsPickSearchDetails.Tables[0];
               gridviewCandidateDetails.DataBind();
               txtSearch.Focus();
           }
           else
           {
               string strScript = "<script>";
               strScript += "alert(''Sorry No records Available'');";
               strScript += "</script>";
               Page.RegisterClientScriptBlock("strScript", strScript);
               gridviewCandidateDetails.Visible = false;
               txtSearch.Focus();
           }
       }<pre>

推荐答案

当您要从该文本框中失去焦点时,将触发服务器端asp.net文本框文本更改。不是每一个角色都会改变而且它会发射。在那种情况下,我的建议是你采取ajax方法。您从javascript处理客户端onkeydown / onkeypress事件,并使用异步get请求(ajax)向服务器发送请求以获取jason数据,并从成功回调接收数据,并使用该数据更新您的ui。它需要一些额外的努力,但如果你这样做,它将为您提供更好的用户体验。你可以去jquery网站获取ajax帮助你也可以查看jquery ui的自动完成工具。
Server side asp.net textbox text changed is fired when you going to lose focus from that textbox. Not every character change and it will fire. In that case my suggestion is you go for ajax approach. You handle client side onkeydown/onkeypress event from javascript and you send request to server for jason data with asynchronous get request(ajax) and receive data from success callback and you update your ui with that data. It need some extra effort but if you go for that it will provide you better user experience. You can go jquery site for ajax help also you can check autocomplete tool for jquery ui.


这篇关于在数据库中搜索名称时应该为文本框触发什么事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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