开发者

asp.net how to refresh gridview

开发者 https://www.devze.com 2023-03-18 22:43 出处:网络
I am trying to refresh the grid-view after insert but it\'s not working for me, here is my code: <asp:TextBox ID=\"TimeBox\" runat=\"server\" />

I am trying to refresh the grid-view after insert but it's not working for me, here is my code:

<asp:TextBox ID="TimeBox" runat="server" />
                <asp:TextBox ID="CommentBox" runat="server" TextMode="MultiLine" />
                    <asp:Button ID="insButton" runat="server" OnClick="insert" Text="Insert" />
<asp:GridView ID="MainGrid" runat="server">
</asp:GridView>

and here is the code behind:

    protected void Page_Load(object sender, EventArgs e)
{
    // filling the grid view

    SqlConnection conn = new SqlConnection (@"connectionString");
    SqlCommand cmd = new SqlCommand("SELECT tim,com FROM ten",conn);
    conn.Open();

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    开发者_Go百科da.Fill(ds);

    MainGrid.DataSource = ds;
    MainGrid.DataBind();  
}

protected void insert(object sender, EventArgs e) //adding the comments 
{
    SqlConnection conn = new SqlConnection(@"connectionString");
    SqlCommand cmd = new SqlCommand("INSERT INTO tennis (tim,com) VALUES (@tim,@com)", conn);
    cmd.Parameters.AddWithValue("@tim", TimBox.Text);
    cmd.Parameters.AddWithValue("@com", ComBox.Text);

    conn.Open();
    cmd.ExecuteNonQuery();
    MainGrid.DataBind();
    conn.Close();
}

the insert works fine and i can see the data if I refresh the page but I am trying to refresh the grid view only without refreshing the page.


Now that you've inserted the record, you need to rebind your datagrid using that new data. I recommend refacoring your code a little so you won't violate the DRY principle (Don't Repeat Yourself) and extract the data call into it's own method.

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        MainGrid.DataSource = GetData();
        MainGrid.DataBind();  
    }
}

protected void insert(object sender, EventArgs e) //adding the comments 
{
    SqlConnection conn = new SqlConnection(@"connectionString");
    SqlCommand cmd = new SqlCommand("INSERT INTO tennis (tim,com) VALUES (@tim,@com)", conn);
    cmd.Parameters.AddWithValue("@tim", TimBox.Text);
    cmd.Parameters.AddWithValue("@com", ComBox.Text);

    conn.Open();
    cmd.ExecuteNonQuery();
    conn.Close();

    MainGrid.DataSource = GetData();
    MainGrid.DataBind();
}

protected DataSet GetData()
{
    SqlConnection conn = new SqlConnection (@"connectionString");
    SqlCommand cmd = new SqlCommand("SELECT tim,com FROM ten",conn);
    conn.Open();

    DataSet ds = new DataSet();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);

    return ds;
}


After inserting the new record, you have to re-query the database and rebind the grid (same as you do in Page_Load) with a datasource which includes the new, inserted record.


You're not seeing the expected results because the event for your insert button is being handled after the grid has been rendered.

See http://msdn.microsoft.com/en-us/library/ms178472.aspx

As a result the data is being added to the table after you've displayed it and why doing an F5 works

Instead move the contents of page load into the PreRender event and all should be good

0

精彩评论

暂无评论...
验证码 换一张
取 消