如何在文本框中验证输入数据。 [英] how to validate input data in textbox.
本文介绍了如何在文本框中验证输入数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void bindgrid()
{
string strConnString = ConfigurationManager.ConnectionStrings [ DatabaseConnectionString 跨度>]的ConnectionString。
SqlConnection conn = new SqlConnection(strConnString);
DataTable dtab = new DataTable();
string query = SELECT [FirstName],[LastName],[Address],[City],[Country],[Age],[Sex],[Status],[Ethnic],[Occupation],[EmailAddress],[Password],[ ImageFilename] FROM [ProfileTbl] Where LastName =' + txtSearch.Text + ' ;
SqlDataAdapter sqlda = new SqlDataAdapter(query,conn);
conn.Open();
sqlda.Fill(dtab);
if (dtab.Rows.Count > 0 )
{
GridView1.DataSource = dtab;
GridView1.DataBind();
}
conn.Close();
受保护 void btnSearch_Click( object sender,EventArgs e)
{
bindgrid();
}
当我从数据库查询LastName(例如:Smith)时,数据库记录中的所有Smith将出现或显示在gridview中。我问题是,如果我搜索不在数据库记录中的琼斯,我只想提示一条消息,即未找到记录..如何验证该语句?是如果还有条件?..
我对你的想法持开放态度。请给我一个想法..
解决方案
嘿
如果您使用了GridView,那么您也可以使用templatefield并编辑EmptyDataTemplate,例如
< asp:GridView ID = GridView1 runat = server AutoGenerateColumns < span class =code-keyword> = False >
< 列 >
< asp:TemplateField HeaderText = 名称 >
< ItemTemplate >
< asp:标签 ID = Label1 runat = server 文字 =' <% #Eval( 名称)%> ' > < / asp:标签 >
< / ItemTemplate >
< / asp:TemplateField >
< /列 >
< EmptyDataTemplate >
< asp:标签 ID = Label2 runat = server 文本 = 找不到数据 > < / asp:标签 >
< / EmptyDataTemplate >
< / asp:Gri dView >
注意::在此代码中Eval(Name)Name是我在SQL中给出的列名
使用条件< pre lang =c#> if (GridView1.Rows.Count < = 0 )
eg->
if (GridView1.Rows.Count < = 0 )
{
return 记录找不到;
}
首先在你的aspx页面上放置scriptmanager
if(dtab。 Rows.Count> 0)
{
GridView1.DataSource = dtab;
GridView1.DataBind( );
}
其他
{
ScriptManager.RegisterStartupScript(this,GetType(),alert,alert(''Record not found'');,true);
}
protected void bindgrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(strConnString);
DataTable dtab = new DataTable();
string query = "SELECT [FirstName],[LastName],[Address],[City],[Country],[Age],[Sex],[Status],[Nationality],[Occupation],[EmailAddress],[Password],[ImageFilename] FROM [ProfileTbl] Where LastName='"+txtSearch.Text+"'";
SqlDataAdapter sqlda = new SqlDataAdapter(query, conn);
conn.Open();
sqlda.Fill(dtab);
if (dtab.Rows.Count > 0)
{
GridView1.DataSource = dtab;
GridView1.DataBind();
}
conn.Close();
protected void btnSearch_Click(object sender, EventArgs e)
{
bindgrid();
}
When I query the LastName (for example:Smith)from database..All Smith in database record will appear or display to the gridview..My question is, if i search "Jones" which is not in the database record, I just want to prompt a message which is "Record not found"..how to validate that statements? is it "if else condition?"..
I am open to your all idea.Give me an idea for this..
解决方案
Hey
If u used the GridView then u can also use templatefield and edit EmptyDataTemplate like
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"> <Columns> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%#Eval("Name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <EmptyDataTemplate> <asp:Label ID="Label2" runat="server" Text="No Data Found"></asp:Label> </EmptyDataTemplate> </asp:GridView>
Note :: In this code "Eval("Name")" Name is the column name which i was given in SQL
use the conditionif (GridView1.Rows.Count <= 0)
eg->
if (GridView1.Rows.Count <= 0) { return "Record not found"; }
First put scriptmanager on your aspx page
if (dtab.Rows.Count > 0)
{
GridView1.DataSource = dtab;
GridView1.DataBind();
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert(''Record not Found'');", true);
}
这篇关于如何在文本框中验证输入数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文