VB.net成功并刷新代码?
我的网站中有一个页面,其中有 5 个不同的文本框,用于将不同的内容添加到我的数据库中。我希望每个人都能够绑定一个成功代码,而且还能够刷新页面。我使用 Response.Write("") 来显示消息,但是当我添加 Response.Redirect("AddToPicklist.aspx")
到它,成功消息不再显示。 ASP.net 是否有更简单的方法来做到这一点?
<tr><td class="title">Image</td></tr>
<tr><td>Image Title: </td></tr>
<tr><td><asp:TextBox ID="txtImageTitle" runat="server"></asp:TextBox></td></tr>
<tr><td><asp:Button ID="btnSubmitImage" runat="server" Text="Submit" /></td></tr>
</table><br />
Protected Sub btnSubmitImage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitImage.Click
**insert code is here but it's not relevant so I'm omitting it**
Response.Write("<script>alert(" & "'Image Title added successfully'" & ")</script>")
Response.Redirect("AddToPicklist.aspx")
I have a page in my site that has 5 different textboxes to add different things to my database. I would like each one to be able to have a success code tied to it, but also be able to refresh the page. I use Response.Write("<script>alert(" & "'Image Title added successfully'" & ")</script>")
to show the message, but when I add Response.Redirect("AddToPicklist.aspx")
to it, the success message doesn't display anymore. Is there an easier way to do this is ASP.net?
<tr><td class="title">Image</td></tr>
<tr><td>Image Title: </td></tr>
<tr><td><asp:TextBox ID="txtImageTitle" runat="server"></asp:TextBox></td></tr>
<tr><td><asp:Button ID="btnSubmitImage" runat="server" Text="Submit" /></td></tr>
</table><br />
Protected Sub btnSubmitImage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmitImage.Click
**insert code is here but it's not relevant so I'm omitting it**
Response.Write("<script>alert(" & "'Image Title added successfully'" & ")</script>")
Response.Redirect("AddToPicklist.aspx")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您在同一个请求中进行重定向,因此您希望输出的 JavaScript 将永远不会被渲染。
一种解决方案是将查询字符串传递到您的下一页,例如:
然后在
AddToPicklist.aspx
上执行:或者,您可以在警报后通过 javascript 重定向到该页面,但这对于这样做的用户来说是不好的没有启用 JavaScript。
顺便说一句,请研究使用
ClientScriptManager.RegisterStartupScript
来输出 javascript,而不是Response.Write
http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
You are redirecting within the same request, therefore the javascript you wish to output will never be rendered.
1 solution would be to pass a querystring to your next page like:
Then on
AddToPicklist.aspx
do:Alternatively you could redirect to the page via javascript after the alert, but this would be bad for users who do not have javascript enabled.
On a side note, look into the use of
ClientScriptManager.RegisterStartupScript
for outputting javascript, rather thanResponse.Write
http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
重定向将使页面(或另一个页面 - 不确定您的页面被称为什么)从头开始重新加载,因此不会再次输入点击事件,并且不会插入
。
我建议不要使用 Response.Write 在客户端上生成消息。至少使用 registerclientscriptblock 例如
(来自 MSDN)
但我会改为考虑使用WebService 写入数据库。然后您就不必进行完整的回发。
The Redirect will be making the page (or another page - not sure what your pages are called) reload from scratch and so the click event will not be entered again and the
<script>
will not be inserted.I would advise against producing messages on the client by using
Response.Write
. At the very least use registerclientscriptblock eg(from MSDN)
But I would instead consider using a WebService to write to your DB. And then you won't have to have a full postback.
我将使用 Response.Write("
I am going to use
Response.Write("<script type='text/javascript'>{ alert('Image Title added successfully'); document.location.href = 'AddToPickList.aspx'; }</script>")