asp.net ajax 异步刷新。在updatepanel外一个按钮点击后刷新updatepanel,为什么我的得到的总是上次的数据
前台:<asp:UpdatePanelID="UpdatePanel1"runat="server"><Triggers><asp:AsyncPostBackTrigge...
前台:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="click" />
</Triggers>
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<%#eval("ID") %>
</ItemTemplate>
</asp:DataList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="textbox1"></asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="BU1"/>
后台
:
Protected Sub page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
conn.Open()
Dim sql2 As String = "select * from test order by ID desc "
Dim cmd2 As New OleDbCommand(sql2, conn)
DataList1.DataSource = cmd2.ExecuteReader()
DataList1.DataBind()
conn.Close()
End Sub
Protected Sub BU1(ByVal sender As Object, ByVal e As EventArgs)
conn.Open()
Dim sql1 As String = "insert into test(content) values (@1)"
Dim cmd1 As New OleDbCommand(sql1, conn)
cmd1.Parameters.Add("@1", OleDbType.Char).Value = TextBox1.Text
cmd1.ExecuteReader()
conn.Close()
End Sub它可以更新,不过当我第一次点击的时候updatepanel1不更新,第2此点击的时候,得到的却是第一次的数据,怎么回事? 展开
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="click" />
</Triggers>
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server">
<ItemTemplate>
<%#eval("ID") %>
</ItemTemplate>
</asp:DataList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="RequiredFieldValidator" ControlToValidate="textbox1"></asp:RequiredFieldValidator>
</ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="BU1"/>
后台
:
Protected Sub page_load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
conn.Open()
Dim sql2 As String = "select * from test order by ID desc "
Dim cmd2 As New OleDbCommand(sql2, conn)
DataList1.DataSource = cmd2.ExecuteReader()
DataList1.DataBind()
conn.Close()
End Sub
Protected Sub BU1(ByVal sender As Object, ByVal e As EventArgs)
conn.Open()
Dim sql1 As String = "insert into test(content) values (@1)"
Dim cmd1 As New OleDbCommand(sql1, conn)
cmd1.Parameters.Add("@1", OleDbType.Char).Value = TextBox1.Text
cmd1.ExecuteReader()
conn.Close()
End Sub它可以更新,不过当我第一次点击的时候updatepanel1不更新,第2此点击的时候,得到的却是第一次的数据,怎么回事? 展开
1个回答
展开全部
if not page.ispostback then
conn.Open()
Dim sql2 As String = "select * from test order by ID desc "
Dim cmd2 As New OleDbCommand(sql2, conn)
DataList1.DataSource = cmd2.ExecuteReader()
DataList1.DataBind()
conn.Close()
endif
conn.Open()
Dim sql2 As String = "select * from test order by ID desc "
Dim cmd2 As New OleDbCommand(sql2, conn)
DataList1.DataSource = cmd2.ExecuteReader()
DataList1.DataBind()
conn.Close()
endif
追问
错了 不过我知道原因了
在ASP.NETAJAX中。BUTTON1点击是不会执行page_load事件的。准确点不是不执行,而是执行了也不向客户端发送数据.所以在我的BUTTON1事件中是先把updatepanel刷新再插入数据。所以我得到的永远是上一次的数据。解决方发在Button1点击事件中在吧数据更新一遍就行了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询