Reputation: 54173
I have some code to show an alert on condition:
protected void btnSave_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvr in gvGroups.Rows)
{
CheckBox cbAdmin = (CheckBox)gvr.FindControl("cbAdmin");
CheckBox cbRemove = (CheckBox)gvr.FindControl("cbRemove");
Label lblID = (Label)gvr.FindControl("lblID");
int id;
bool idValid = int.TryParse(lblID.Text,out id);
bool isReadOnly = !cbAdmin.Checked;
if (idValid)
{
Group g = SecurityManager.GetGroup(id);
if (g.IsReadOnly != isReadOnly)
{
bool updateSuccess = SecurityManager.ChangeGroupPermissions(id, isReadOnly);
var master = Master as KezberProjectManager;
if (master != null)
{
master.ShowErrorAlert("Sample Error");
}
}
if (cbRemove.Checked)
{
bool removeEmpSuccess = SecurityManager.RemoveEmployeesFromGroup(id);
bool removeSuccess = SecurityManager.RemoveGroup(id);
}
}
}
BindGrid();
}
And this calls:
public void ShowErrorAlert(string message)
{
error_alert.Visible = true;
lblError.Text = message;
}
When the master page loads:
if (!IsPostBack)
{
success_alert.Visible = false;
error_alert.Visible = false;
}
The only issue is, once the alert is shown, even if I dont ask for it to be shown next time, it is still there. The way I need it is, 1 call to ShowErrorAlert should only show it once, after, if I click the save button but no changes are made, then the show method is not called and it should not show up.
Is there a way to do this?
Thanks
Upvotes: 0
Views: 78
Reputation: 2730
Once you show the alert, there is no option in your code to hide it since you hide it only on !IsPostback So you can just hide the alert in the beginning of the button click event by default and show it only if the condition is true..
Upvotes: 0
Reputation: 17395
If you want to show the alert only on one postback, remove the if (!IsPostBack)
before setting the visibility of the success_alert
and error_alert
to false.
Page_Load
is called before btnSave_Click
so you want to first set them to Visible = false;
and then, if ShowErrorAlert()
is called, it'll change it to true.
Upvotes: 2