Make error div only shown when set to visible? - c#

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

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.

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

Related

How to make a button invisible even when I refresh a page depending on a label content in xamarin forms

When I click on the button it becomes hidden, however when I refresh the page it is still there. How to make it stay hidden depending on a content in a label?
private void Button3_Clicked(object sender, EventArgs e)
{
App.products[Index].Quantity++;
Button btn = (Button)sender;
if (btn.IsVisible)
{ btn.IsVisible = false; }
else
{
btn.IsVisible = false;
}
}
I want this Button to stay hidden when the page is refreshed depending on this value App.products[Index].Quantity. When I click on the Button it becomes from 0 to 1 and I want if it is not 0 the Button to be hidden.
In YourPage.xaml:
<Button IsVisible={Binding ButtonIsVisible} ... />
In YourPage.xaml.cs:
public YourPage()
{
InitializeComponent();
...
BindingContext = this;
}
// NOTE: `=>` not `=`. To be a property; expression evaluated every time it is needed.
public bool ButtonIsVisible => App.products[Index].Quantity == 0;
private void Button3_Clicked(object sender, EventArgs e)
{
App.products[Index].Quantity++;
// So xaml sees the change.
OnPropertyChanged(nameof(ButtonIsVisible));
}
For more information, google xamarin binding.

Xamarin C#: ImageButton, deselecting a button upon selecting another one

I'm building a game, one of the 'mechanics' is selecting a tool by clicking on one of the three ImageButtons (representing a Drill, a Hammer and a Brush).
I want to mark the clicked button selected but only until I switch tools by selecting a second one. Is there any clean way of unselecting/selecting buttons?
Current code:
if (drillbutton.Selected)
{
brushbutton.Selected = false;
hammerbutton.Selected = false;
}
else if(hammerbutton.Selected)
{
drillbutton.Selected = false;
brushbutton.Selected = false;
}
else if(brushbutton.Selected)
{
drillbutton.Selected = false;
hammerbutton.Selected = false;
}
There must be an easier way... Right?
You could assign the same click handler to every button, like this:
private void SelectButton(object sender, EventArgs args) {
// you will have to have an IEnumerable that contains your set of buttons
foreach(var b in buttons) {
if (b == (Button)selected) b.Selected = true else b.Selected = false;
}
}

Disable repositoryItemCheckEdit after it's checked

I have a repositoryItemCheckEdit in a column of my grid. The task I want to do is :
Once the user pressed the CheckEdit , this cell become disable so that the user can not make click again.
To do this task I'm using the CheckedChanged event, in the following way :
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
var obj = sender as CheckEdit;
if (obj.Checked)
{
repositoryItemCheckEdit1.Enabled = false;
}
}
With the above event the only thing I get is that the cell becomes clearer , but not is disabled. Even if I make click again it allows me to do it.
Any help is appreciated.
You will probably have more luck/an easier time dealing with this using the brute force method... at least i find this a lot easier than dealing with the crazyness of DataGridView controls scheme.
Use the Tag attribute of your control to set a flag on it, and then when someone tries to un-check it/change it, force it back to checked. Like so:
private void repositoryItemCheckEdit1_CheckedChanged(object sender, EventArgs e)
{
var obj = sender as CheckEdit;
if(obj.Tag != null)
{
obj.Checked = true;
repositoryItemCheckEdit1.Enabled = false;
}
else
{
if (obj.Checked)
{
obj.Tag = true;
repositoryItemCheckEdit1.Enabled = false;
}
}
}

Enabling all buttons in a repeater except for the clicked one

I have a Repeater Control with various buttons in it.
When the button gets clicked, it needs to disable itself so it cant be clicked again. Working.
However, when I click that button, it needs to enable any other button but it.
So, When I click on it, it needs to disable. When I click on another one, the previous button must enable, and that one must disable.
So for I've tried:
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Button b in e.Item.Controls.OfType<Button>().Select(c => c).Where(b => b != btnLoad))
{
b.Enabled = true;
}
btnLoad.Text = "Currently Viewing";
btnLoad.Enabled = false;
But it isnt working. Depending on where I put it, its either leaving all the buttons enabled (But still changing its text), or not doing anything at all.
What do I need to do to make this work?
Edit: The code is found here:
protected void rptPdfList_ItemCommand(object source, RepeaterCommandEventArgs e)
Which is why I use Button btnLoad = (Button)e.Item.FindControl("btnLoad");.
The method is found in :
switch (e.CommandName)
{
case "LoadDoc":
//Above code
break;
}
Assuming you want that code in the Button's Click-event handler:
protected void Button_Clicked(Object sender, EventArgs e)
{
Button thisButton = (Button) sender;
thisButton.Text = "Currently Viewing";
RepeaterItem item = (RepeaterItem) thisButton.NamingContainer;
IEnumerable<Button> buttons = item.Controls.OfType<Button>();
foreach(var btn in buttons)
{
btn.Enabled = btn != thisButton;
}
}
Try a simple if statement in a loop as below
Button btnLoad = (Button)e.Item.FindControl("btnLoad");
foreach (Control c in e.Item.Controls)
{
if (b is Button)
{
if(b == btnLoad)
{
b.Enabled = false;
}
else
{
b.Enabled = true;
}
}
}

Button Submit calls Onload first!

I have a problem with a webform.
My Goal: Intially when a page is loading, it has to load every textbox empty. After filling the info and click submit, it has to get submitted(UpdatePaymentInfo())
Problem: Here, When the user fills the info and clicks Submit,it calls onload function even before the submit button and makes all text box empty.
Here is the code:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string QueryStringupdatecreditcard1 = Request.QueryString.ToString();
if (String.Equals(QueryStringupdatecreditcard1, "tabID=B"))
{
divTitle.Visible = false;
trmain.Visible = false;
tdOrderSummary.Visible = false;
trCCandBilling.Visible = true;
trtest2.Visible = false;
divUpdatecreditcard.Visible = true;
trusecompaddress.Visible = false;
txtFirstName.Text = "";
txtLastName.Text = "";
txtAddress1.Text = "";
txtAddress2.Text = "";
txtCity.Text = "";
txtZip.Text = "";
txtCardNo.Text = "";
txtVccNumber.Text = "";
trAmountCharged.Visible = false;
}
}
protected void imgbtnSubmit_Click(object sender, ImageClickEventArgs e)
{
try
{
UpdatePaymentInfo();
}
}
Wrap the current contents of your OnLoad method in:
if (!Page.IsPostBack)
{
// Code in here will only be executed when the page is *not* being loaded by a postback
}
This is because, as per the ASP.NET Page Life Cyle, the things that you care about in this instance happen in this order:
Load - During load, if the current request is a postback, control
properties are loaded with information
recovered from view state and control
state.
Postback event handling - If the request is a postback, control event
handlers are called. After that, the
Validate method of all validator
controls is called, which sets the
IsValid property of individual
validator controls and of the page.
So what happens is (somewhat simplified):
You click the image button, triggering the postback.
The data from your form is loaded into your controls.
Your OnLoad method overwrites the values in the controls to clear them.
Your click handler is run, but because of step 3 it sees empty values.
As others have sort-of mentioned, it wouldn't necessarily be a bad thing to refactor your OnLoad method whilst you're doing this. At the moment you seem to have it doing two distinct things:
Clearing the text fields
Setting the visibility of fields
It might be worth separating this into one or two (depending on if the visibility setting and field clearing will be done independently) separate methods and adjusting your OnLoad method so it looks like this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsInPostBack)
{
SetFieldVisibility();
ClearFields();
}
}
Page_Load always occurs.
See the documentation on the Page Lifecycle
What you need to do is check to see if the Page_Load is being triggered by a Postback.
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///do stuff in here that you want to occur only on the first lad.
}
else
}
// code that you want to execute only if this IS a postback here.
{
}
// do stuff you want to do on Page_Load regardless of postback here.
}
You can use the IsPostBack property of the Page as follows:
protected override void OnLoad(EventArgs e) {
if (!Page.IsPostBack) {
EmptyTextBoxes();
}
}
Have you tried wrapping the form reset code in a check to see if the page is a postback?
if(!Page.IsPostback) {
// Do form reset here
}
You thought about using the IsPostBack page variable?
protected override void OnLoad(EventArgs e)
{
if(!IsPostBack){
//all your logic here.
}
}
if it's the case, you might use a databound control and set it to insert mode

Categories