views:

3291

answers:

2

I've been having troubles getting my textbox to refresh a GridView from the onchange event.

The GridView is linked up to a LINQ data source and the LINQ data source has a Where Parameter UserId that it gets from the textbox... Here's the code:

    <asp:Label ID="label_UserId" runat="server" Text="Search by User Id: "></asp:Label>
    <asp:TextBox ID="textbox_UserId" Text="12" runat="server" 
        ontextchanged="textbox_UserId_TextChanged"></asp:TextBox>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="UserID" DataSourceID="LINQUserSource" 
        EmptyDataText="There are no data records to display.">
        <Columns>
            <asp:BoundField DataField="UserID" HeaderText="UserID" ReadOnly="True" 
                SortExpression="UserID" />
            <asp:BoundField DataField="Username" HeaderText="Username" 
                SortExpression="Username" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        </Columns>
    </asp:GridView>

    <asp:LinqDataSource ID="LINQUserSource" runat="server" 
        ContextTypeName="DotNetNuke.Modules.Report.UsersDataContext" 
        Select="new (UserID, Username, FirstName, LastName, Email)" Where="UserId = @UserId"
        TableName="Users">
        <WhereParameters>
            <asp:ControlParameter
                Name="UserId"
                DefaultValue="0"
                ControlID="textbox_UserId"
                Type="Int32" /> 
        </WhereParameters>
    </asp:LinqDataSource>

So far I have nothing for the backend code. Since I set the textbox to be 12 by default, the GridView loads with the record of UserId 12, but now I want the GridView to reload if I change the number in the textbox. Any ideas?

Thanks,
Matt

+1  A: 

Just call

GridView1.DataBind();

after you enter new value.

Jacob
+2  A: 

First add the AutoPostBack property to your TextBox:

<asp:TextBox ID="textbox_UserId" Text="12" runat="server" 
    AutoPostBack="true" ontextchanged="textbox_UserId_TextChanged"/>

Then, put this in your code behind:

protected void textbox_UserId_TextChanged(object sender, EventArgs e)
{
    GridView1.DataBind();
}
drs9222