How to make my OnClientClick execute within pre-existing if statements? - c#

I have an asp.net web app and one of my pages is for payment. I have a submit button that will only work if 1) you have selected a currency in which you wish to pay (done with radio buttons) and 2) all of the payment fields have been filled out (card & billing info). This all works great, the last thing missing is if a currency has been selected and all payment info is filled out, I wanted a confirmation box with 'OK' and 'Cancel' buttons. I added the following code into my page_load method: submitPayBtn.OnClientClick = "return confirm('Are you sure you wish to change the name?');";. The issue is that the message box appears whenever I click the submit button, ignoring my if statements already in place.
Is there a way to enable/disable the OnClientClick where applicable within my if statements, so that the box won't appear if a currency hasn't been selected and/or not all of the payment information has been filled out? Cheers!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Coursework
{
public partial class Payment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
submitPayBtn.OnClientClick = "return confirm('Are you sure you wish to change the name?');";
if (!Page.IsPostBack)
{
Type cstype = this.GetType();
// Get a ClientScriptManager reference from the Page class.
ClientScriptManager cs = Page.ClientScript;
// Check to see if the startup script is already registered.
if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
{
String cstext = "alert('Details are correct, welcome');";
cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);
}
}
}
protected void submitPayBtn_Click(object sender, EventArgs e)
{
if (!poundRadBtn.Checked && !usdolRadBtn.Checked && !ozdolRadBtn.Checked && !eurRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text == "" || cardBox2.Text == "" || cardBox3.Text == "" || cardBox4.Text == "" || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please select a currency in which you wish to pay');</script>");
}
if (poundRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working gbp');</script>");
Response.Redirect("Home.aspx");
}
if (usdolRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working usd');</script>");
Response.Redirect("Home.aspx");
}
if (ozdolRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working aud');</script>");
Response.Redirect("Home.aspx");
}
if (eurRadBtn.Checked)
if (cardList.Text == "" || cardNameBox.Text == "" || cardBox1.Text.Length < 4 || cardBox2.Text.Length < 4 || cardBox3.Text.Length < 4 || cardBox4.Text.Length < 4 || expMonList.Text == "" || expYrList.Text == "" || billNameBox.Text == "" || billAdd1Box.Text == "" || billAdd2Box.Text == "" || billCtyBox.Text == "" || billPostBox.Text == "" || billCntryBox.Text == "")
{
Response.Write("<script>alert('Please ensure all fields have an entry (including a 16 digit card number)');</script>");
}
else if (cardList.SelectedIndex > -1 && cardNameBox.Text.Length > 0 && cardBox1.Text.Length > 3 && cardBox2.Text.Length > 3 && cardBox3.Text.Length > 3 && cardBox4.Text.Length > 3 && expMonList.SelectedIndex > -1 && expYrList.SelectedIndex > -1 && billNameBox.Text.Length > 0 && billAdd1Box.Text.Length > 0 && billAdd2Box.Text.Length > 0 && billCtyBox.Text.Length > 0 && billPostBox.Text.Length > 0 && billCntryBox.Text.Length > 0)
{
Response.Write("<script>alert('Working eur');</script>");
Response.Redirect("Home.aspx");
}
}
protected void backBtn_Click(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}
}
}

You should move the return confirm('Are you sure you wish to change the name?'); to a separate javascript function. Then you can change it based on other form values.
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" OnClientClick="return myFunction()" />
<script type="text/javascript">
function myFunction() {
if (document.getElementById("<%= TextBox1.ClientID %>").value != "") {
return confirm('Are you sure you wish to change the name?');
} else {
return true;
}
}
</script>
And I suggest you take a look at the build-in aspnet Validation Controls

Related

Only state once

I am making an RPG sort of text adventure in Visual Studio C# console.
I am trying to create a piece of code that will detect your location. I want it to then respond in the necessary format. I know you can probably do this more effectively with arrays but I'm happy with how it is now. What I want to do is stop it from constantly repeating the message "It worked.". If it only states it once then it has worked. I have tried multiple methods. Please offer a solution to my issues.
Code Here or Underneath
using System;
namespace RPG
{
class MainClass
{
public static void Main(string[] args)
{
//Done(); will check if Cords are at end of game
//variables
string response = "";
string name = "";
bool hastyped = false;
//Coordinates
int xCord = 0; int yCord = 0;
void Done()
{
if (yCord == 5 && xCord == 7)
{
Console.WriteLine("Thanks for playing! You did it!");
}
}
void Move()
{
if (hastyped == true)
{
if (response == "E")
{
xCord = xCord + 1;
}
else if (response == "W")
{
xCord = xCord - 1;
}
else if (response == "N")
{
xCord = xCord + 1;
}
else if (response == "S")
{
xCord = xCord - 1;
}
else
{
Console.WriteLine("That movement is invalid.");
}
hastyped = false;
response = "";
Done();
}
}
//FANCY START LOGO
Console.WriteLine(#"
____________ _____
| ___ \ ___ \ __ \
| |_/ / |_/ / | \/
| /| __/| | __
| |\ \| | | |_\ \
\_| \_\_| \____/
"
);
System.Threading.Thread.Sleep(2500);
while (response != "Y")
{
Console.WriteLine("Please answer with Y for yes and N for no. Please try to answer all questions as simply as possible.\nPlease type N for north, E for east, S for south and W for west.");
Console.WriteLine("Ok?");
response = Console.ReadLine();
if (response == "Y")
{
Console.WriteLine("Well lets start:");
}
else
{
Console.WriteLine("Please try again.");
}
}
//Instructions
Console.WriteLine("LOADING...");
Console.WriteLine("7%");
System.Threading.Thread.Sleep(200);
Console.WriteLine("26 %");
System.Threading.Thread.Sleep(100);
Console.WriteLine("48%");
System.Threading.Thread.Sleep(200);
Console.WriteLine("76%");
System.Threading.Thread.Sleep(600);
Console.WriteLine("87%");
System.Threading.Thread.Sleep(400);
Console.WriteLine("99%");
System.Threading.Thread.Sleep(20);
Console.WriteLine("100%");
//Loading...
Console.WriteLine("Hey buddy, you took that hit pretty hard. Are you alright?");
Console.ReadLine();
Console.WriteLine("Well we need to get you to a hospital.\nSorry, what was your name?");
name = Console.ReadLine();
Console.WriteLine("Well " + name + ", I wish we were meeting under better circumstances.");
Console.WriteLine("Anyway, I'm Dave. Off to the East is a path that looks pretty safe, the only other direction is back to the west where I just found you. So we won't go there.");
Console.WriteLine("YOU MOVED EAST!!!");
xCord = xCord + 1;
//Intro
//GAMEPLAY
while (true)
{
//1,0
if (xCord == 1 && yCord == 0)
{
Console.WriteLine("Hit E then enter for testing.");
response = Console.ReadLine();
hastyped = !string.IsNullOrWhiteSpace(response);
if (hastyped == true)
{
Move();
}
}
//2,0
if (xCord == 2 && yCord == 0)
{
Console.WriteLine("It worked.");
}
//3,0
if (xCord == 3 && yCord == 0)
{
}
//-1,0
if (xCord == -1 && yCord == 0)
{
}
//-2,0
if (xCord == -2 && yCord == 0)
{
}
//-3,0
if (xCord == -3 && yCord == 0)
{
}
//1,1
if (xCord == 1 && yCord == 1)
{
}
//2,1
if (xCord == 2 && yCord == 1)
{
}
//3,1
if (xCord == 3 && yCord == 1)
{
}
//-1,1
if (xCord == -1 && yCord == 1)
{
}
//-2,1
if (xCord == -2 && yCord == 1)
{
}
//-3,1
if (xCord == -3 && yCord == 1)
{
}
//1,2
if (xCord == 1 && yCord == 2)
{
}
//2,2
if (xCord == 2 && yCord == 2)
{
}
//3,2
if (xCord == 3 && yCord == 2)
{
}
//-1,2
if (xCord == -1 && yCord == 2)
{
}
//-2,2
if (xCord == -2 && yCord == 2)
{
}
//-3,2
if (xCord == -3 && yCord == 2)
{
}
//1,3
if (xCord == 1 && yCord == 3)
{
}
//2,3
if (xCord == 2 && yCord == 3)
{
}
//3,3
if (xCord == 3 && yCord == 3)
{
}
//-1,3
if (xCord == -1 && yCord == 3)
{
}
//-2,3
if (xCord == -2 && yCord == 3)
{
}
//-3,3
if (xCord == -3 && yCord == 3)
{
}
//1,-1
if(xCord == 1 && yCord == -1)
{
}
//2,-1
if (xCord == 2 && yCord == -1)
{
}
//3,-1
if (xCord == 3 && yCord == -1)
{
}
//-1,-1
if (xCord == -1 && yCord == -1)
{
}
//-2,-1
if (xCord == -2 && yCord == -1)
{
}
//-3,-1
if (xCord == -3 && yCord == -1)
{
}
//1,-2
if (xCord == 1 && yCord == -2)
{
}
//2,-2
if (xCord == 2 && yCord == -2)
{
}
//3,-2
if (xCord == 3 && yCord == -2)
{
}
//-1,-2
if (xCord == -1 && yCord == -2)
{
}
//-2,-2
if (xCord == -2 && yCord == -2)
{
}
//-3,-2
if (xCord == -3 && yCord == -2)
{
}
//1,-3
if (xCord == 1 && yCord == -3)
{
}
//2,-3
if (xCord == 2 && yCord == -3)
{
}
//3,-3
if (xCord == 3 && yCord == -3)
{
}
//-1,-3
if (xCord == -1 && yCord == -3)
{
}
//-2,-3
if (xCord == -2 && yCord == -3)
{
}
//-3,-3
if (xCord == -3 && yCord == -3)
{
}
//0,3
if (xCord == 0 && yCord == 3)
{
}
//0,2
if (xCord == 0 && yCord == 2)
{
}
//0,1
if (xCord == 0 && yCord == 1)
{
}
//0,0
if (xCord == 0 && yCord == 0)
{
}
//0,-1
if (xCord == 0 && yCord == -1)
{
}
//0,-2
if (xCord == 0 && yCord == -2)
{
}
//0,-3
if (xCord == 0 && yCord == -3)
{
}
//BOUNDARIES:
//RIGHT
if (xCord == 4)
{
Console.WriteLine("This area doesn't exist.");
xCord = xCord - 1;
}
//LEFT
if (xCord == -4)
{
Console.WriteLine("This area doesn't exist.");
xCord = xCord + 1;
}
//TOP
if (yCord == 4)
{
Console.WriteLine("This area doesn't exist.");
yCord = yCord - 1;
}
//BOTTOM
if (yCord == -4)
{
Console.WriteLine("This area doesn't exist.");
yCord = yCord + 1;
}
}
}
}
}
This is a lot of code in one main loop, I would suggest splitting the functionality into more functions.
To answer your question; the reason your console is spamming "It Worked!". Is because your "if" statement is inside a continuous while-loop. You could try adding an extra boolean to your if statement, as such:
//GAMEPLAY
boolean hasMoved = false;
while (true)
{
//2,0
if (xCord == 2 && yCord == 0 && !hasMoved)
{
Console.WriteLine("It worked.");
hasMoved = true;
}
else{
hasMoved = false;
}
why dont you use switch case in place of if else .
also, you can use break; after printing it worked

I am trying to delete the records in excel from column 1 to 35

// Here I am trying to delete all the rows starting from column 1
to 35.But while deleting the records, some records are left behind and
its not deleting all the rows. I am unable to understand where the
problem actually is.Is it the problem with the task manager but already I am killing the exe files. I am looping through all the rows from second row till the last row and hence if the records are not empty,I am deleting the records with the help of get_range one by one.
public static void GetIncidentExcel(string Incident_Path, List<excelObj> ListTickets)
{
lastRow = 0;
MyApp = new Excel.Application();
// MyApp.Visible = false;
MyBook = MyApp.Workbooks.Open(Incident_Path);
MySheet = (Excel.Worksheet)MyBook.Sheets["BOXI_summary"]; // Explict cast is not required here
lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
for (int i = 2; i <= lastRow; i++)
{
Array MyValues = (Array)MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString()).Cells.Value;
if (Convert.ToString(MyValues.GetValue(1, 1)) == "" && Convert.ToString(MyValues.GetValue(1, 2)) == "" && Convert.ToString(MyValues.GetValue(1, 3)) == "" &&
Convert.ToString(MyValues.GetValue(1, 4)) == "" && Convert.ToString(MyValues.GetValue(1, 5)) == "" && Convert.ToString(MyValues.GetValue(1, 6)) == "" &&
Convert.ToString(MyValues.GetValue(1, 7)) == "" && Convert.ToString(MyValues.GetValue(1, 8)) == "" && Convert.ToString(MyValues.GetValue(1, 9)) == "" &&
Convert.ToString(MyValues.GetValue(1, 10)) == "" && Convert.ToString(MyValues.GetValue(1, 11)) == "" && Convert.ToString(MyValues.GetValue(1, 12)) == "" &&
Convert.ToString(MyValues.GetValue(1, 13)) == "" && Convert.ToString(MyValues.GetValue(1, 14)) == "" && Convert.ToString(MyValues.GetValue(1, 15)) == "" &&
Convert.ToString(MyValues.GetValue(1, 16)) == "" && Convert.ToString(MyValues.GetValue(1, 17)) == "" && Convert.ToString(MyValues.GetValue(1, 18)) == "" &&
Convert.ToString(MyValues.GetValue(1, 19)) == "" && Convert.ToString(MyValues.GetValue(1, 20)) == "" && Convert.ToString(MyValues.GetValue(1, 21)) == "" &&
Convert.ToString(MyValues.GetValue(1, 22)) == "" && Convert.ToString(MyValues.GetValue(1, 23)) == "")
break;
else
{
Excel.Range cells = MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString());
cells.Delete();
}
}
Try replacing your loop with something like this:
for (int i = lastRow; i >= 2; i--)
{
if (MyApp.WorksheetFunction.CountA(MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString())) == 0)
{
MySheet.get_Range("A" + i.ToString(), "AI" + i.ToString()).Delete(Excel.XlDirection.xlUp);
}
}
You can use the WorksheetFunction object to check if all the cells are empty, and when deleting rows you should always loop backwards

C# Validation of Period

I want to ask if anyone knows how to limit the period allowed for a textbox. I'm using C# Visual Studio 2010.
My problem is I need to find validation codes that will ensure that only a single period is allowed for the textbox Middle Initial of the user. And if the user type another period, then the period will not be shown in the textbox. No error messages is needed. Example is the validation code that accept letters only. Here is my code for this example:
private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
{
byte num = Convert.ToByte(e.KeyChar);
if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122) || (num == 8) || (num == 32))
{
}
else if (num == 13)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
else
{
e.Handled = true;
}
}
I have the following codes currently for my txtboxMI:
private void txtMI_KeyPress(object sender, KeyPressEventArgs e)
{
byte num = Convert.ToByte(e.KeyChar);
if ((num >= 65 && num <= 90) || (num >= 97 && num <= 122) || (num == 8) || (num == 32))
{
}
else if (num == 13)
{
e.Handled = true;
SendKeys.Send("{Tab}");
}
else
{
e.Handled = true;
}
}
Do you have to make it server-side?
You can use regular expression validator.
[a-zA-Z_-.] should give you what you want.
Try this:
var txt = (TextBox)sender;
if ((e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == 8 || e.KeyChar == 32) {
} else if (txt.Text.Contains('.') && e.KeyChar == '.') {
e.Handled = true;
} else if (e.KeyChar == '\t') {
e.Handled = true;
SendKeys.Send("{Tab}");
}

Keypress Upper/Lowercase exceptions

i have a little tiny problem, what i am trying to do is limit my textBox to the following characters: [a=>f, x, A=>F, 0=>9], and what i need exactly is add an exception that will make any lower case input in the mentioned textBox become uppercase, except for "x", this is what i tried, but it limited all inputs from the textBox:
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar < 'A' || e.KeyChar > 'F') && (e.KeyChar < 'a' || e.KeyChar > 'f') && (e.KeyChar != ' '))
{
e.Handled = true;
textBox1.CharacterCasing = CharacterCasing.Upper;
}
else if ((e.KeyChar != 'x'))
{
e.Handled = true;
textBox1.CharacterCasing = CharacterCasing.Lower;
}
Thank you.
managed to bypass it:
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar < 'A' || e.KeyChar > 'F') && (e.KeyChar < 'a' || e.KeyChar > 'f') && (e.KeyChar != ' ') && (e.KeyChar != 'x'))
{
e.Handled = true;
}
//textBox1.CharacterCasing = CharacterCasing.Upper;
if (e.KeyChar == 'x') e.KeyChar = Char.ToLower(e.KeyChar);
else e.KeyChar = Char.ToUpper(e.KeyChar);
thank you.

C#: Simplifying code

I'm a big fan of simplifying code to enable extensibility. I have tried to simplify this piece of code but I just keep getting stuck. Does anyone know how I can simplify this code as it contains a lot of ugly if if if's. The reason for the over complications is the amount of variables being passed in to the method. These are all required.
Thanks
private void SetVisibility(int AuctionID, int BidStatus, int LoginErrorCode, int IsHome, bool IsGetMoreTokens)
{
this._dealBidPlacedControl.Visible = false;
this._dealBidControl.Visible = false;
this._loginReg.Visible = false;
this._deals.Visible = false;
if (AuctionID > 0)
{
if (LoginErrorCode == 0)
{
if (BidStatus > 0)
{
this._dealBidPlacedControl.Visible = true;
}
else
{
this._dealBidControl.Visible = true;
}
}
else
{
this._loginReg.LoginErrorType = LoginErrorCode;
this._loginReg.Visible = true;
this._deals.Visible = true;
}
}
else
{
this._loginReg.Visible = true;
this._deals.Visible = true;
}
if (IsGetMoreTokens)
{
this._getMoreTokens.Visible = true;
this._loginReg.Visible = false;
this._deals.Visible = false;
}
// set the hidden field which can be used to set visibility if included in a tab control or anything
// by the parent site
if (this.AuctionID > 0 || BidStatus > 0 || IsHome > 0 || LoginErrorCode > 0 || IsGetMoreTokens)
this._visibilityStatus.Value = "1";
}
You can use boolean expressions, like this:
this._dealBidPlacedControl.Visible = AuctionId > 0 && LoginErrorCode == 0 && BidStatus > 0;
this._dealBidControl.Visible = AuctionId > 0 && LoginErrorCode == 0 && BidStatus <= 0;
this._loginReg.Visible = AuctionID > 0 && LoginErrorCode != 0;
this._deals.Visible = LoginErrorCode != 0 && !IsGetMoreTokens;
Also, you can use enums or booleans instead of ids and codes. HasAuction is clearer than AuctionId > 0.
You could try to simplify your code with directly setting the boolean values instead of creating if-statements and assigning true:
_dealBidPlacedControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0
Example:
bool hasAuction = AuctionID > 0;
bool hasLoginError = LoginErrorCode != null;
bool hasBidStatus = BidStatus > 0;
this._dealBidPlacedControl.Visible = hasAction && !hasLoginError && hasBidStatus;
this._dealBidControl.Visible = hasAction && !hasLoginError && !hasBidStatus;
this._loginReg.Visible = this._deals.Visible = !hasAction || hasLoginError;
if (hasAuction && hasLoginError)
{
this._loginReg.LoginErrorType = LoginErrorCode;
}
if (IsGetMoreTokens)
{
this._getMoreTokens.Visible = true;
this._loginReg.Visible = false;
this._deals.Visible = false;
}
// set the hidden field which can be used to set visibility if included in a tab control or anything
// by the parent site
if (this.AuctionID > 0 || BidStatus > 0 || IsHome > 0 || LoginErrorCode > 0 || IsGetMoreTokens)
this._visibilityStatus.Value = "1";
How about taking it the other way around?
private void SetVisibility(int AuctionID, int BidStatus, int LoginErrorCode, int IsHome, bool IsGetMoreTokens)
{
this._dealBidPlacedControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0;
this._dealBidControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus <= 0;
this._loginReg.Visible = AuctionID > 0 && LoginErrorCode != 0;
this._deals.Visible = AuctionID > 0 && LoginErrorCode != 0;
etc.
}
You could even box all the parameters to an auxiliary class:
class VisibilityParameters
{
public int AuctionID;
public int BidStatus;
...
}
so that your method becomes easier to extend in future:
private void SetVisibility( VisibilityParamteres parameters )
{
}
This gives you the chance to extract boolean expressions out of the method body:
private void SetVisibility( VisibilityParameters parameters )
{
this._dealBidPlacedControl.Visible = DealBidPlaceVisible( parameters );
etc.
}
private bool DealBidPlaceVisible( VisibilityParameters parameters )
{
return parameters.AuctionID > 0 && parameters.LoginErrorCode == 0 && parameters.BidStatus > 0
}
Personally I like to move the code which control the appearance closer to the control. Avoding cluttering the code with lots of gui logic, like this:
private void SetVisibility(int AuctionID, int BidStatus, int LoginErrorCode, int IsHome, bool IsGetMoreTokens)
{
this._dealBidPlacedControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0;
this._dealBidControl.Visible = AuctionID > 0 && LoginErrorCode == 0 && BidStatus <= 0;
this._loginReg.LoginErrorType = LoginErrorCode;
this._loginReg.Visible = !IsGetMoreTokens && LoginErrorCode != 0;
this._deals.Visible = !IsGetMoreTokens && LoginErrorCode != 0;
this._getMoreTokens.Visible = IsGetMoreTokens;
// set the hidden field which can be used to set visibility if included in a tab control or anything
// by the parent site
if (this.AuctionID > 0 || BidStatus > 0 || IsHome > 0 || LoginErrorCode > 0 || IsGetMoreTokens)
this._visibilityStatus.Value = "1";
}
And if you have an expensive operation you could always cache it in a local variable, but this is how I would do it.
if (AuctionID > 0 && LoginErrorCode == 0 && BidStatus > 0)
{
this._dealBidPlacedControl.Visible = true;
}
else if (AuctionID > 0 && LoginErrorCode == 0 && AuctionID <= 0)
{
this._dealBidControl.Visible = true;
}
else if (AuctionID > 0 && LoginErrorCode != 0)
{
this._loginReg.LoginErrorType = LoginErrorCode;
this._loginReg.Visible = true;
this._deals.Visible = true;
}
else if(AuctionID <= 0)
{
this._loginReg.Visible = true;
this._deals.Visible = true;
}

Categories

Resources