2016-08-25 54 views
1

請我希望有一個場景,我將能夠使用文本框來搜索數據,並與一個GridView使用文本框,並與一個GridView

<%結果顯示下拉條結果顯示下拉條搜索數據@頁面語言= 「C#」 AutoEventWireup = 「真」 代碼隱藏= 「WebForm1.aspx.cs中」 繼承= 「WebApplication1.WebForm1」 %>

</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)); 


     } 
    } 


} 

// OK

+0

採取參考[這裏](http://www.c-sharpcorner.com/blogs/search-gridview-by-dropdownlist-and-textbox-value1) – BNN

回答

0

試試這個,在page2.aspxPage_Load事件

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 [email protected],[email protected],[email protected]",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(); 

希望幫助,

+0

不要使用'AddWithValue'。 http://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/和https://msdn.microsoft.com/en-us/magazine/ee236412的.aspx – VDWWD

相關問題