I have a simple ASP.NET page:
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
When I click the button, the page reload, all the data have no change, I must re-enter the page again, the record I have delete disappear. Can you show me why?
The full code here:
<%@ 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><%#Containe开发者_StackOverflow中文版r.DataItem("userphone")%> </td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</form>
<hr />
</body>
</html>
In asp page life cycle, page load happens before the button click (I know, it's kind of strange). The easiest work around is to place your code in the page "PreRender" event.
Use if(!IsPostBack) on page load.
You have not added the !IsPostBack
condition in your page load event. When you click the button your page's load event is first called before the click event handler and it will reload the data again. That's the issue.
It should be like...
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
try this
<%@ 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>
精彩评论