2011-05-31 145 views
0

我有一個簡單的ASP.NET頁面:的Page_Load不更新後點擊按鈕

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> 

回答

1

在asp頁面生命週期中,頁面加載發生在按鈕點擊之前(我知道,這有點奇怪)。最簡單的解決方法是將代碼放入頁面「PreRender」事件中。

+0

@nvcnvn,這就是答案! – Alaa 2012-11-06 13:28:20

+0

請檢查我的答案作爲答案。謝謝 – boruchsiper 2012-11-06 17:14:27

1

你有沒有在你的頁面加載事件添加!IsPostBack條件。當你點擊按鈕時,你的頁面加載事件首先在click事件處理程序之前被調用,並且它會重新加載數據。這是問題。

應該是這樣......

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

使用,如果(!的IsPostBack)在頁面加載。

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> 
+0

:((沒幫助! – nvcnvn 2011-06-03 03:54:52

+0

現在你遇到了什麼問題 – 2011-06-03 05:01:48