你好,
其实你可以使用数据库语言select like来实现你的需求。
然后从后台重新绑定grdview。
具体你可以参照如下代码:
ASPX:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id">
<Columns>
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="password" HeaderText="password" SortExpression="password" />
<asp:BoundField DataField="CityId" HeaderText="CityId" SortExpression="CityId" />
<asp:BoundField DataField="ClassId" HeaderText="ClassId" SortExpression="ClassId" />
<asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
</Columns>
</asp:GridView>
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MysqlConnectionString %>" SelectCommand="SELECT * FROM [Userinfo]"></asp:SqlDataSource>
后台代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = ConfigurationManager
.ConnectionStrings["MysqlConnectionString"].ConnectionString;
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select * from [Userinfo] where " +
"name like @SearchText + '%'";
cmd.Parameters.AddWithValue("@SearchText", TextBox1.Text);
cmd.Connection = conn;
DataTable d1 = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(d1);
GridView1.DataSource = d1;
GridView1.DataBind();
}
}
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to
MSDN Support, feel free to contact MSDNFSF@microsoft.com.