如何在asp.net C#中基于Dropdownlist,Textbox和Search按钮显示数据 [英] how to display data based on Dropdownlist , Textbox and Search button in asp.net C#
问题描述
嗨专家,
我正在使用Asp.net C#,SqlServer 2005.
on我的网页我有Gridview来显示它工作正常的数据。
我有Dropdownlist,TextBox,Button根据Dropdownlist中选择的列搜索记录。
我的列名为FirstName,LastName,UserName ......这些将在Dropdownlist中
用户将选择FirstName从下拉列表中,在TextBox中输入FirstName,然后单击搜索按钮。
这是我的搜索按钮代码
====== ===========================
sqlconnection con = new sqlconnection(_connString);
sqlcommand cmd = new sqlCommand( 选择*来自TableName,其中FirstName =' + TxtSearch.Text + ',con );
sqldataadapter da = new sqldataadapter(cmd);
数据集ds = new dataset();
da.fill(ds);
GVDisplay.Datasource = ds;
GVDisplay.DataBind();
所以,我的要求是如何使用基于下拉列表。
请帮忙。
谢谢
< pre lang =c#> SqlConnection con = new SqlConnection(_connString);
SqlCommand cmd = null ;
if (DropDownList1.SelectedItem.Text == FirstName)
{
cmd = new SqlCommand( 从TableName中选择*,其中FirstName =' + TxtSearch.Text + ',con);
}
else if (DropDownList1.SelectedItem.Text == LastName)
{
cmd = 新 SqlCommand( 从TableName中选择*,其中LastName =' + TxtSearch.Text + ',con);
}
else if (DropDownList1.SelectedItem.Text == UserName)
{
cmd = 新 SqlCommand( 从TableName中选择*,其中UserName =' + TxtSearch.Text + ',con);
}
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Dispose();
DataSet ds = new DataSet();
da.fill(ds);
GVDisplay.Datasource = ds;
GVDisplay.DataBind();
非常简单的尝试以下代码...
sqlconnection con = new sqlconnection(_connString);
sqlcommand cmd = new sqlCommand( 选择*来自TableName,其中[ + DropDownList1.SelectedItem.Text + ] =' + TxtSearch.Text + ',con);
sqldataadapter da = new sqldataadapter(cmd);
数据集ds = new dataset();
da.fill(ds);
GVDisplay.Datasource = ds;
GVDisplay.DataBind();
我刚刚更改了一行代码
sqlcommand cmd = new sqlCommand( 从TableName中选择*其中[ + DropDownList1.SelectedItem.Text + ] =' + TxtSearch。文字+ ',con);
希望它对你有用......
Hi Experts,
am working on Asp.net C#, SqlServer 2005.
on my webpage I have Gridview to display data it working fine.
I have Dropdownlist, TextBox, Button To Search the Records based on column selected in Dropdownlist.
I have column names as FirstName,LastName,UserName...These will be in Dropdownlist
User will select FirstName from dropdownlist and enter FirstName in TextBox and clicks on Search button.
This is my code for Search button
=================================
sqlconnection con = new sqlconnection(_connString);
sqlcommand cmd = new sqlCommand("Select * from TableName where FirstName='" + TxtSearch.Text + "'",con);
sqldataadapter da = new sqldataadapter(cmd);
dataset ds = new dataset();
da.fill(ds);
GVDisplay.Datasource = ds;
GVDisplay.DataBind();
So, My Requirement is how to Work with Based on Dropdownlist.
Please help.
Thanks
SqlConnection con = new SqlConnection(_connString); SqlCommand cmd = null; if (DropDownList1.SelectedItem.Text == "FirstName") { cmd = new SqlCommand("Select * from TableName where FirstName='" + TxtSearch.Text + "'", con); } else if (DropDownList1.SelectedItem.Text == "LastName") { cmd = new SqlCommand("Select * from TableName where LastName='" + TxtSearch.Text + "'", con); } else if (DropDownList1.SelectedItem.Text == "UserName") { cmd = new SqlCommand("Select * from TableName where UserName='" + TxtSearch.Text + "'", con); } SqlDataAdapter da = new SqlDataAdapter(cmd); cmd.Dispose(); DataSet ds = new DataSet(); da.fill(ds); GVDisplay.Datasource = ds; GVDisplay.DataBind();
Its very simple try following code...
sqlconnection con = new sqlconnection(_connString); sqlcommand cmd = new sqlCommand("Select * from TableName where ["+DropDownList1.SelectedItem.Text+"] = '" + TxtSearch.Text + "'",con); sqldataadapter da = new sqldataadapter(cmd); dataset ds = new dataset(); da.fill(ds); GVDisplay.Datasource = ds; GVDisplay.DataBind();
I have just changed only one line of your code
sqlcommand cmd = new sqlCommand("Select * from TableName where [" + DropDownList1.SelectedItem.Text + "] = '" + TxtSearch.Text + "'",con);
Hope it works for you...
这篇关于如何在asp.net C#中基于Dropdownlist,Textbox和Search按钮显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!