简体   繁体   中英

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. 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")

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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