【问题标题】:Searching data using Text box and Drop Down with result displaying on a gridview使用文本框和下拉搜索数据,结果显示在网格视图上
【发布时间】:2017-01-01 18:24:19
【问题描述】:

我想要一个场景,我将能够使用文本框和下拉搜索数据,结果显示在网格视图上


</script>  

    <table>
        <tr>
            <td>Last Name</td>
            <td>
                <asp:TextBox ID="txtLastName" runat="server"></asp:TextBox></td>
        </tr>
        <tr>
            <td>Speciality</td>
            <td>
                <asp:DropDownList ID="ddlSpeciality" runat="server">
                    <asp:ListItem Text="" />
                    <asp:ListItem Text="ItemA" />
                    <asp:ListItem Text="ItemB" />
                    <asp:ListItem Text="ItemC" />
                </asp:DropDownList></td>
        </tr>
        <tr>
            <td>Location</td>
            <td>
                <asp:DropDownList ID="ddlLocation" runat="server">
                    <asp:ListItem Text="" />
                    <asp:ListItem Text="Item1" />
                    <asp:ListItem Text="Item2" />
                    <asp:ListItem Text="Item3" />
                </asp:DropDownList></td>
        </tr>
        <tr>

            <td colspan="2" align="center">
                <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click" /></td>
        </tr>
    </table>


</form> </body> </html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            string lastName = txtLastName.Text.Trim();
            string speciality = ddlSpeciality.Text;
            string location = ddlLocation.Text;
            Response.Redirect(string.Format("page2.aspx?lastname={0}&speciality={1}&location={2}",lastName ,speciality , location));


        }
    }


}

//确定

【问题讨论】:

  • here获取参考

标签: c# asp.net gridview


【解决方案1】:

试试这个,在 page2.aspx Page_Load event

string lastname = Request.QueryString["lastname"];
string location = Request.QueryString["location"];
string speciality = Request.QueryString["speciality"];
SqlConnection cnn = new SqlConnection("Server=.;Database=db;Trusted_Connection=True;");
SqlDataAdapter adp = new SqlDataAdapter("select * from Table where lastname=@lname,location=@loc,speciality=@spec",cnn);
cnn.Open();
adp.SelectCommand.Parameters.AddWithValue("@lname", lastname);
adp.SelectCommand.Parameters.AddWithValue("@loc", location);
adp.SelectCommand.Parameters.AddWithValue("@spec", speciality);
DataTable dt = new DataTable();
adp.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

希望有所帮助,

猜你喜欢
  • 1970-01-01
  • 2012-05-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-24
  • 2015-03-07
  • 2014-08-30
相关资源
最近更新 更多