【问题标题】:Search data in textbox based on dropdownlist selected value根据下拉列表选择值在文本框中搜索数据
【发布时间】:2015-02-08 21:10:29
【问题描述】:

我在 asp.net gridview 中创建了一个公司联系人列表。我需要图片中的内容。如果有人以前这样做过,请给我一个链接或一些示例代码。

【问题讨论】:

  • 1) 你的问题不够清楚。 2)我们不为你工作,这是一个社区的努力,所以要有耐心......如果你没有耐心,那就试着自己学习。

标签: c# asp.net search


【解决方案1】:

aspx代码

<asp:Label ID="Label1" runat="server" Text="Search By"></asp:Label>

    <asp:TextBox ID="txtSearchName" runat="server"></asp:TextBox>

   <asp:DropDownList ID="DropDownList1" runat="server">
    <asp:ListItem>Name</asp:ListItem>
        <asp:ListItem>Search By Employee Name</asp:ListItem>
        <asp:ListItem>Search By Company Name</asp:ListItem>            
        <asp:ListItem>Search By Mobile</asp:ListItem>
    </asp:DropDownList>

    <asp:Button ID="btnSearch_Click" runat="server" onclick="btnSearch_Click_Click" 
        Text="Search By" />

c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlCon = new   SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        SqlCommand cmd = new SqlCommand("Select * from tablename", sqlCon);
        sqlCon.Open();
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
    }
}


protected void btnSearch_Click_Click(object sender, EventArgs e)
{

    string Query = string.Empty;
    try
    {
        if (sqlCon.State == ConnectionState.Closed)
        {
            sqlCon.Open();
        }
        if (DropDownList1.SelectedValue.ToString() == "Search By Employee Name")
        {
            Query = "select * from tablename where EmployeeName Like '%" + txtSearchName.Text + "%'";
        }
        else if (DropDownList1.SelectedValue.ToString() == "Search By Company Name")
        {
            Query = "select * from tablename where CompanyName Like '" + txtSearchName.Text + "%'";
        }
        else if (DropDownList1.SelectedValue.ToString() == "Search By Mobile")
        {
            Query = "select * from tablename where Mobile Like '%" + txtSearchName.Text + "'";
        }

        SqlDataAdapter sqlDa = new SqlDataAdapter(Query, sqlCon);
        DataSet Ds = new DataSet();
        sqlDa.Fill(Ds);
        GridView1.DataSource = Ds;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        HttpContext.Current.Response.Write("<script>alert('wfrmGrid: 11')</script>" + ex.Message);
    }
    finally
    {
        sqlCon.Close();
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-01
    • 2014-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    相关资源
    最近更新 更多