I am trying to filter my gridview using textbox within my gridiview headerTemplate. I am currently using TextChanged event method, to execute this task, however when I execute the events method, I am unable to filter the gridiview based on the search input, in the 'txtID'.
protected void grdAdjAMT_TextChanged(object sender, EventArgs e)
{
TextBox txtName = (TextBox)GridView1.Rows[(0)].FindControl("txtID");
string strConnString = ConfigurationManager.ConnectionStrings["####"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select u.[uID], u.[uForenames],
u.[uSurname], u.[uCompany], u.[uEmailAddress],
s.[sStartDate]
from [dbo].[UserDe]
where u.[uID] like '%" + txtName + "%'
order by s.[sStartDate] desc";
cmd.Connection = con;
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
BindGrid();
}
I debugged my script and found that the debugger only goes through the pageload and BindGrid method, but it does not go through the "grdAdjAMT_TextChanged" method. I also tried to debug the textChange method separately, and still nothing happens.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="uID" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White" CellPadding="4" ForeColor="#333333" GridLines="None" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<HeaderTemplate>
<asp:Label ID="Label1" runat="server" Text="ID"></asp:Label><br />
<asp:TextBox ID="txtID" runat="server" OnTextChanged="grdAdjAMT_TextChanged" AutoPostBack="true"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("uID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Any further advice would be very much appreciated. Thanks
TextChangedevent is raised when the text changes between posts to the server - are you expecting this to post back as the text changes on the client side?__doPostBackjacascript method This sounds like you need to add someif(IsPostBack){}checks in thePage_LoadEvent alsoFYIyou do not need to callcon.Close();since it's nested inside theusing(){}Ajax