Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have an gird which should refresh after a new data is being inserted into it,for which i used the update panel,but the problem is the update panel is not refreshing after the click event.
C#
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <p>
        &nbsp;</p>
    <p><asp:ScriptManager ID="MainScriptManager" runat="server" />
        <asp:UpdatePanel ID="pnlHelloWorld" runat="server"  UpdateMode="Conditional">
      
            <ContentTemplate>

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
                    DataSourceID="SqlDataSource1">
                    <Columns>
                        <asp:BoundField DataField="username" HeaderText="username" 
                            SortExpression="username" />
                        <asp:BoundField DataField="comments" HeaderText="comments" 
                            SortExpression="comments" />
                    </Columns>
                </asp:GridView>

            <asp:Button ID="Button2" runat="server" onclick="Button2_Click" 
        style="position: relative; top: 0px; left: 0px; width: 379px" Text="Button" 
                    />
                 
            
            </ContentTemplate>

               <Triggers>
                <asp:AsyncPostBackTrigger ControlID="Button2" EventName="Click" />
                </Triggers>
            </asp:UpdatePanel>

my code behind click event:


C#
protected void Button2_Click(object sender, EventArgs e)
        {
            string username = TextBox1.Text;
            string comments = TextBox2.Text;
            SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ForumDBConnectionString"].ConnectionString);//create our sql command object
            SqlCommand cmd = new SqlCommand("Insert INTO tests(username,comments) VALUES (@username, @comments)", conn);
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@comments", comments);

            using (conn)
            {
                conn.Open();
                cmd.ExecuteNonQuery();
            }

pls help me out
Posted
Updated 4-May-12 23:15pm
v3

1 solution

Bind your grid after inserting value
C#
using (conn)
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                gridview1.DataBind();
            }
 
Share this answer
 
v2
Comments
ashok_89 5-May-12 5:20am    
Thanks a lot sir, i have one doubt if we use the repeater control instead of grid do we need to do any bindings because initially i was using the repeater control
uspatel 5-May-12 5:22am    
Yes.Whenever you insert data in datbase,you should rebind it.
ashok_89 5-May-12 5:26am    
Dear sir i searched in internet i couldnt find the bind option for repeater control,can u please tell me
uspatel 5-May-12 5:38am    
See this
http://msdn.microsoft.com/en-us/library/s1th73f0.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900