1

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?

4 Answers 4

5

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();
}
2

Just call

GridView1.DataBind();

after you enter new value.

0
OleDbCommand cmd = new OleDbCommand("update ESInfo1 set OrdTime='" + td.ToString() + "', SSO2='" + DropSupportOfficer.Text + "', Orderby='" + Txthst.Text + "' where Sln=" + Txtsln.Text + "", con);
cmd.ExecuteNonQuery();
GridView1.DataBind();
cmd.Dispose()

but no row present in gridview

1
  • It's unclear to me how this solves the problem (if at all), would you mind explaining your answer further?
    – RobV
    Oct 26, 2012 at 19:09
-1

create a datatable method and return

GridView1.DataSource = method(); GridView1.DataBind();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.