首次在Button1_Click中执行代码后,在UpdatePanel中刷新GridView

时间:2014-04-24 20:19:27

标签: asp.net gridview updatepanel

我想在执行Button1_Click中的代码后更新UpdatePanel1。

UpdatePanel1.Update()未更新GridView1。

我认为UpdatePanel1中的触发器在按下Button1之后立即触发,而不是在按钮事件中执行代码之后触发。

首先在Button1_Click中执行代码后,我想在UpdatePanel1中刷新GridView1。

Default.aspx.vb

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
    Dim connStr As String = "connection string works"
    Dim cmdStr As String = "INSERT INTO [test] ([datetime],[col1],[col2],[col3]) VALUES (@datetime,@col1,@col2,@col3);"
    Try
        Using conn As New SqlConnection(connStr)
            Using cmd As New SqlCommand(cmdStr, conn)
                conn.Open()
                cmd.Parameters.AddWithValue("@datetime", DateTime.Now)
                cmd.Parameters.AddWithValue("@col1", TextBox1.Text)
                cmd.Parameters.AddWithValue("@col2", TextBox2.Text)
                cmd.Parameters.AddWithValue("@col3", TextBox3.Text)
                cmd.ExecuteNonQuery()
                conn.Close()
                cmd.Dispose()
                conn.Dispose()
            End Using
        End Using
    Catch ex As Exception

    End Try
    UpdatePanel1.Update()
End Sub

Default.aspx的

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click"></asp:AsyncPostBackTrigger>
    </Triggers>
    <ContentTemplate>
        <center><table><tr><td align="center">
                <asp:Label ID="Label4" runat="server" Text="Column 1"></asp:Label></td><td align="center">
                <asp:Label ID="Label5" runat="server" Text="Column 2"></asp:Label></td><td align="center">
                <asp:Label ID="Label6" runat="server" Text="Column 3"></asp:Label><br /></td></tr><tr><td align="center">
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td><td align="center">
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td><td align="center">
                <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /></td></tr><tr><td colspan="3" align="center">
                <asp:Button ID="Button1" runat="server" Text="Insert Into Gridview" /></td></tr><tr><td><br />
                <asp:GridView ID="GridView1" 
                    DataSourceID="SqlDataSource1"
                    AutoGenerateColumns="true"
                    AllowPaging="true"
                    PageSize="50" 
                    AllowSorting="true"
                    DataKeyNames="idt"
                    AutoGenerateEditButton="true" 
                    AutoGenerateDeleteButton="true"
                    runat="server">
                </asp:GridView>
                <asp:SqlDataSource 
                    ID="SqlDataSource1"
                    ConnectionString="<%$ ConnectionStrings:connone %>"
                    SelectCommand="SELECT * FROM [test];"
                    DataSourceMode="DataSet"
                    UpdateCommand="UPDATE test [datetime],[col1],[col2],[col3] SET @datetime, @col1, @col2, @col3;"
                    DeleteCommand="DELETE FROM [test] WHERE [idt]=@idt;"
                    runat="server">
                </asp:SqlDataSource>
        </td></tr></table></center>
    </ContentTemplate>
</asp:UpdatePanel>

1 个答案:

答案 0 :(得分:0)

您不需要在Button1_Click方法中调用UpdatePanel1.Update(),因为Button1是UpdatePanel的触发器。相反,您必须调用GridView1.DataBind()来从DB重新获取数据,如下所示:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
Dim connStr As String = "connection string works"
Dim cmdStr As String = "INSERT INTO [test] ([datetime],[col1],[col2],[col3]) VALUES (@datetime,@col1,@col2,@col3);"
Try
    Using conn As New SqlConnection(connStr)
        Using cmd As New SqlCommand(cmdStr, conn)
            conn.Open()
            cmd.Parameters.AddWithValue("@datetime", DateTime.Now)
            cmd.Parameters.AddWithValue("@col1", TextBox1.Text)
            cmd.Parameters.AddWithValue("@col2", TextBox2.Text)
            cmd.Parameters.AddWithValue("@col3", TextBox3.Text)
            cmd.ExecuteNonQuery()
            conn.Close()
            cmd.Dispose()
            conn.Dispose()
        End Using
    End Using
    Catch ex As Exception

End Try
GridView1.Databind()
End Sub