简体   繁体   English

VB.net是否成功并刷新代码?

[英]VB.net success and refresh code?

I have a page in my site that has 5 different textboxes to add different things to my database. 我的网站中有一个页面,其中包含5个不同的文本框,可将不同的内容添加到数据库中。 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. 我使用Response.Write("<script>alert(" & "'Image Title added successfully'" & ")</script>")来显示消息,但是当我添加Response.Redirect("AddToPicklist.aspx")对此,成功消息不再显示。 Is there an easier way to do this is ASP.net? 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")

You are redirecting within the same request, therefore the javascript you wish to output will never be rendered. 您正在同一请求内进行重定向,因此,您希望输出的javascript将永远不会呈现。

1 solution would be to pass a querystring to your next page like: 一种解决方案是将查询字符串传递给您的下一页,例如:

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

Then on AddToPicklist.aspx do: 然后在AddToPicklist.aspx执行以下操作:

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. 另外,您也可以在警报发出后通过javascript重定向到该页面,但这对于未启用javascript的用户来说是不利的。


On a side note, look into the use of ClientScriptManager.RegisterStartupScript for outputting javascript, rather than Response.Write 附带说明一下,研究使用ClientScriptManager.RegisterStartupScript来输出javascript,而不是Response.Write

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

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. 重定向将使页面(或其他页面-不确定您的页面叫什么)从头开始重新加载,因此不会再次输入click事件,并且不会插入<script>

I would advise against producing messages on the client by using Response.Write . 我建议不要使用Response.Write在客户端上生成消息。 At the very least use registerclientscriptblock eg 至少使用registerclientscriptblock例如

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

(from MSDN) (来自MSDN)

But I would instead consider using a WebService to write to your DB . 但是我会考虑使用WebService来写入数据库 And then you won't have to have a full postback. 然后,您将不必进行完整的回发。

我将使用Response.Write("<script type='text/javascript'>{ alert('Image Title added successfully'); document.location.href = 'AddToPickList.aspx'; }</script>")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM