I have a C# page that is writing some records to a database when the user clicks a button. The user wanted a popup confirmation that the records were written successfully. So I added a HiddenField to the page and wrote some simple javascript to check the value of the HiddenField and display the value of the field if it’s not blank.
I added this to the ASPX page just after the <Form> tag:
<asp:HiddenField ID=”MsgTxt” runat=”server” Value=”" />
<script language=”javascript” type=”text/javascript”>
var AlertMsg = document.getElementById(‘<%=MsgTxt.ClientID%>’).value;
if (!AlertMsg == ”) {
alert(AlertMsg);
}
</script>
I added this to the button that writes the records to the database:
MsgTxt.Value = “Message”;
I added this to the Page_Load to clear the variable so it’s only displayed once:
if (Page.IsPostBack)
{
MsgTxt.Value = “”;
}
That should do it. It’s a simple change to display any type of alert message after a postback.
August 22, 2011 at 7:48 am
Absolutely fantastic