OK - I think this should be simple/basic but I looked through many posts with similar issues but it is still not clear how to do this.
Basic setup is as follows - I have a TextBox called "product" on the page which posts back on text change.
In page load I do the following:
protected void Page_Load(object sender, EventArgs e)
{
prods.SelectCommand = "SELECT id, pID, pName FROM prods WHERE (pID LIKE '%" + product.Text + "%')";
prodsTable.DataBind();
}
The above does not update the table using the new SelectCommand value. From some other posts - I think this may have to do with Page loading event sequence and maybe table binding does not happen?
BTW, I also tried to do this using parameters (code below) - that does not work either. Maybe there is a simple fix to that (compared to programmatically changing the above to work?)
Thanks in advance - I would like to get it working either way (Programmatically or parametrically).
Full code is below:
<asp:TextBox ID="product" runat="server" AutoPostBack="True"></asp:TextBox>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="id" DataSourceID="prods" AllowPaging="True"
AllowSorting="True" AutoGenerateEditButton="True">
<Columns>
<asp:BoundField DataField="pID" HeaderText="Product ID" ReadOnly="True"
SortExpression="pID" />
<asp:BoundField DataField="pName" HeaderText="Product Name"
SortExpression="pName" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ProviderName="MySql.Data.MySqlClient" ID="prods" runat="server"
CancelSelectOnNullParameter="false" ConnectionString="<%$ mystring %>"
SelectCommand="SELECT id, pID, pName FROM prods WHERE (pID LIKE '%@productFilter%')">
<SelectParameters>
<asp:ControlParameter ControlID="product" DefaultValue=""
Name="productFilter" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>