单击按钮后,Page_Load不会更新

时间:2011-05-31 16:54:34

标签: asp.net vb.net

我有一个简单的ASP.NET页面:

sub Page_Load
    //Get data form databse and show it
end sub

sud deletsome(Source As Object, e As EventArgs)
    //delete one record when user click on submit button
end sub

当我点击按钮,页面重新加载时,所有数据都没有变化,我必须重新进入页面,我删除的记录消失了。 你能告诉我为什么吗?

此处的完整代码:

<%@ Import Namespace="System.Data.OleDb" %>

<script  runat="server">

sub Page_Load
    dim dbconn,sql,dbcomm,dbread
    dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
    dbconn.Open()
    sql="SELECT * FROM [user]"
    dbcomm=New OleDbCommand(sql,dbconn)
    dbread=dbcomm.ExecuteReader()
    customers.DataSource=dbread
    customers.DataBind()
    dbread.Close()
    dbconn.Close()
end sub

sub deletesome(Source As Object, e As EventArgs)
    dim dbconn,sql,dbcomm
    dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
    dbconn.Open()
    sql="DELETE FROM [user]WHERE id = @ID"
    dbcomm=New OleDbCommand(sql,dbconn)
    dbcomm.Parameters.AddWithValue("ID", tb1.Text)
    dbcomm.ExecuteNonQuery()    
end sub
</script>

<html>
<body>

<form runat="server">
    <asp:TextBox id="tb1" runat="server" />
    <asp:Button id="b1" Text="Submit" runat="server" OnClick="deletesome" />
    <asp:Repeater id="customers" runat="server">

        <HeaderTemplate>
            <table border="1" width="100%">
            <tr bgcolor="#b0c4de">
                <th>ID</th>
                <th>Address</th>
                <th>City</th>
            </tr>
        </HeaderTemplate>

        <ItemTemplate>
            <tr bgcolor="#f0f0f0">
                <td><%#Container.DataItem("id")%> </td>
                <td><%#Container.DataItem("username")%> </td>
                <td><%#Container.DataItem("userphone")%> </td>
            </tr>
        </ItemTemplate>

        <FooterTemplate>
            </table>
        </FooterTemplate>

    </asp:Repeater>
</form>
<hr />

</body>
</html>

4 个答案:

答案 0 :(得分:1)

您尚未在页面加载事件中添加!IsPostBack条件。当您单击按钮时,首先在单击事件处理程序之前调用页面的加载事件,它将再次重新加载数据。这就是问题所在。

应该是......

sub Page_Load
IF(!IsPostBack) ' This condition will be true when the page loads the first time 

dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
dbconn.Open()
sql="SELECT * FROM [user]"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()

EndIf
end sub

答案 1 :(得分:1)

在页面加载时使用if(!IsPostBack)。

答案 2 :(得分:1)

在asp页面生命周期中,页面加载发生在按钮点击之前(我知道,这有点奇怪)。最简单的解决方法是将代码放在“PreRender”事件页面中。

答案 3 :(得分:0)

试试这个

    <%@ Import Namespace="System.Data.OleDb" %>

    <script  runat="server">

    sub Page_Load
        If Page.IsPostBack = False Then
            LoadData()
    end sub

    sub deletesome(Source As Object, e As EventArgs)
        dim dbconn,sql,dbcomm
        dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
        dbconn.Open()
        sql="DELETE FROM [user]WHERE id = @ID"
        dbcomm=New OleDbCommand(sql,dbconn)
        dbcomm.Parameters.AddWithValue("ID", tb1.Text)
        dbcomm.ExecuteNonQuery()  

        LoadData()  
    end sub

    sub LoadData
        dim dbconn,sql,dbcomm,dbread
        dbconn=New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Persist Security Info=False;data source=" & server.mappath("/data/test.accdb"))
        dbconn.Open()
        sql="SELECT * FROM [user]"
        dbcomm=New OleDbCommand(sql,dbconn)
        dbread=dbcomm.ExecuteReader()
        customers.DataSource=dbread
        customers.DataBind()
        dbread.Close()
        dbconn.Close()
    end sub
    </script>

    <html>
    <body>

    <form runat="server">
        <asp:TextBox id="tb1" runat="server" />
        <asp:Button id="b1" Text="Submit" runat="server" OnClick="deletesome" />
        <asp:Repeater id="customers" runat="server">

            <HeaderTemplate>
                <table border="1" width="100%">
                <tr bgcolor="#b0c4de">
                    <th>ID</th>
                    <th>Address</th>
                    <th>City</th>
                </tr>
            </HeaderTemplate>

            <ItemTemplate>
                <tr bgcolor="#f0f0f0">
                    <td><%#Container.DataItem("id")%> </td>
                    <td><%#Container.DataItem("username")%> </td>
                    <td><%#Container.DataItem("userphone")%> </td>
                </tr>
            </ItemTemplate>

            <FooterTemplate>
                </table>
            </FooterTemplate>

        </asp:Repeater>
    </form>
    <hr />

    </body>
    </html>