VB.net成功并刷新代码?

发布于 2024-12-05 17:59:48 字数 994 浏览 1 评论 0原文

我的网站中有一个页面,其中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南街女流氓 2024-12-12 17:59:48

您在同一个请求中进行重定向,因此您希望输出的 JavaScript 将永远不会被渲染。

一种解决方案是将查询字符串传递到您的下一页,例如:

Response.Redirect("AddToPicklist.aspx?alert=titleadded")

然后在 AddToPicklist.aspx 上执行:

If Request.QueryString("alert") = "titleadded" Then
   Response.Write("<script>alert(" & "'Image Title added successfully'" & ")</script>")
End If

或者,您可以在警报后通过 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:

Response.Redirect("AddToPicklist.aspx?alert=titleadded")

Then on AddToPicklist.aspx do:

If Request.QueryString("alert") = "titleadded" Then
   Response.Write("<script>alert(" & "'Image Title added successfully'" & ")</script>")
End If

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 than Response.Write

http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx

朱染 2024-12-12 17:59:48

重定向将使页面(或另一个页面 - 不确定您的页面被称为什么)从头开始重新加载,因此不会再次输入点击事件,并且不会插入

我建议不要使用 Response.Write 在客户端上生成消息。至少使用 registerclientscriptblock 例如

RegisterClientScriptBlock("showSaveMessage", _
  "<script language=""JavaScript""> _
      alert('Your changes have been saved.'); _
   </script>")

(来自 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

RegisterClientScriptBlock("showSaveMessage", _
  "<script language=""JavaScript""> _
      alert('Your changes have been saved.'); _
   </script>")

(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.

梦归所梦 2024-12-12 17:59:48

我将使用 Response.Write("

I am going to use Response.Write("<script type='text/javascript'>{ alert('Image Title added successfully'); document.location.href = 'AddToPickList.aspx'; }</script>")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文