page.Isvalid always return false? - c#

I have two buttons and two separate validation groups for each button. I kept EnableClientScript=false on each requiredfield validator of the textboxes. I have c# code like below
void submitButton_Click(object sender, EventArgs e)
{
this.Page.Validate("LoginAccountGroup");
if (this.Page.IsValid)
{
}
}
void saveButton_Click(object sender, EventArgs e)
{
this.Page.Validate("CreateAccountGroup");
if (Page.IsValid)
{
}
}
My question is this Page.Isvalid is always returning false in the c# code. How to make it work

Try This
protected bool IsGroupValid(string sValidationGroup)
{
Page.Validate(sValidationGroup);
foreach (BaseValidator validator in Page.GetValidators(sValidationGroup))
{
if (!validator.IsValid)
{
return false;
}
}
return true;
}

Related

Calling a private void function from a button click method

I am trying to call a private void function from a button click method . The value of selectedChoice is fetched from combo box . I debugged and found out that the value of selectedChoice is being fetched properly and it is even going inside If condition inside the button click method. Its just that the functions are not being call .
Every method is inside the class Form1.
namespace Test
{
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
if(selectedChoice == "ABC")
{
Function1();
Function2();
}
The combo box code -
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex!=0)
{
selectedChoice = comboBox1.SelectedItem.ToString();
}
}
The functions I am trying to call are simply defined as
private void Function1()
{
//do something
}
You are trying to perform a compare opertion
selectedChoice == "ABC"
the
==
Operator shall not be used for string. This operator will check if the id of the element and the comparer are equals. Please use
selectedChoice.Equals("ABC")
instead.
Nevertheless the Methods are called using this code:
private string selectedChoice = "ABC";
private void button1_Click(object sender, EventArgs e)
{
if(selectedChoice.Equals("ABC"))
{
Function1();
Function2();
}
}
private void Function2()
{
}
private void Function1()
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if(comboBox1.SelectedIndex!=0)
{
selectedChoice = comboBox1.SelectedItem.ToString();
}
}

c# how to make a tabpage visibe to only some users based on roles

private void tabPage3_Click(object sender, EventArgs e)
{
((Control)this.tabPage3).Enabled = false;
}
private void tabPage4_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
((Control)this.tabPage4).Visible = false;
this.tabPage4.Hide();
}
}
private void tabPage1_Click(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabPage1.Hide();
}
}
private void WelcomePage_Load(object sender, EventArgs e)
{
I've done this some time ago. The problem is that there is no property Visible and Enabled is doing not the things you would like to do.
So here is how i'm doing it:
// Put this over the constructor
private TabPage tabPage4ToShowForNotStudents = this.tabPage4;
private TabPage tabPage1ToShowForNotStudents = this.tabPage1;
Then you have to subscribe the Load-Method of your Form:
void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role != "student")
{
yourTabControl.TabPages.Add(this.tabPage4ToShowForNotStudents);
yourTabControl.TabPages.Add(this.tabPage1ToShowForNotStudents);
}
}
Now it will add the TabPage to your TabControl if the Role is not student. If it is it will not be added.
Be sure to not have them added in the designer otherwise it will not work.
Hope this is useful :)
thanks guys.i have sorted out the problem.
private void WelcomePage_Load(object sender, EventArgs e)
{
if (GloballsClass.Role == "student")
{
tabControl1.TabPages.Remove(tabPage5);
}
else
{
tabControl1.TabPages.Remove(tabPage4);
}
}

EventHandler for all controls in form

In my code I want to perform some actions when some controls are focused. So instead of having one handler for each control i was wondering if there could be any way of adding all controls to the handler and inside the handler function perform the desired action.
I have this:
private void tb_page_GotFocus(Object sender, EventArgs e)
{
tb_page.Visible = false;
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
private void tb_maxPrice_GotFocus(Object sender, EventArgs e)
{
tb_maxPrice.Text = "";
}
I want this:
private void AnyControl_GotFocus(Object sender, EventArgs e)
{
if(tb_page.isFocused == true)
{
...
}
else if (tb_maxPrice.isFocused == true)
{
...
}
else
{
...
}
}
Is this possible? How could I do it? Thanks a lot.
Iterate your controls in your form or panel and subscribe to their GotFocus Event
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control c in this)
{
c.GotFocus += new EventHandler(AnyControl_GotFocus);
}
}
void AnyControl_GotFocus(object sender, EventArgs e)
{
//You'll need to identify the sender, for that you could:
if( sender == tb_page) {...}
//OR:
//Make sender implement an interface
//Inherit from control
//Set the tag property of the control with a string so you can identify what it is and what to do with it
//And other tricks
//(Read #Steve and #Taw comment below)
}

Ensure page is accessed from a specific link

Say if I have link1.aspx and link2.aspx. Within link1.aspx, I redirect the user to link2.aspx.
What is the most efficient way of checking that link2.aspx is only accessed via link1.aspx?
For example, something like:
link2.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if page is not accessed via link1.aspx
{
Response.Redirect("~/portal.aspx");
}
}
}
I could use a query string but are there any other ways?
You can use UrlReferrer. However, it is not a secure way of detecting where the user comes from.
For example,
if (string.Equals(Request.UrlReferrer.AbsoluteUri,
"YOUR_REFERRER_URL",
StringComparison.InvariantCultureIgnoreCase))
{
}
If it is redirecting between pages inside your application, I would like to suggest to use SessionState which is more secure and robust than UrlReferrer.
link1.aspx.cs
private bool IsValidUrl
{
set { Session["IsValidUrl"] = true; }
}
protected void Button1_Click(object sender, EventArgs e)
{
IsValidUrl = true;
Response.Redirect("link2.aspx");
}
link2.aspx.cs
private bool IsValidUrl
{
get
{
if (Session["IsValidUrl"] != null)
return Convert.ToBoolean(Session["IsValidUrl"]);
return false;
}
set { Session["IsValidUrl"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsValidUrl)
{
// user comes from valid url.
// .... Do somthing
// Reset session state value
IsValidUrl = false;
}
}
You could use the Request.UrlReferrer property to check what page the user is coming from.

c#, listbox, stackOverflow exception

I inherited KryptonToolkit ListBox control to get SelectedItemChanging event.
public class CPListBox : KryptonListBox
{
public event CancelEventHandler SelectedIndexChanging;
protected virtual void OnSelectedIndexChanging(CancelEventArgs e)
{
SelectedIndexChanging(this, e);
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
CancelEventArgs cArgs = new CancelEventArgs();
OnSelectedIndexChanged(cArgs);
if(!cArgs.Cancel)
{
base.OnSelectedIndexChanged(e);
}
}
}
In form I handle event with code:
void UsersListBoxSelectedIndexChanging(object sender, CancelEventArgs e)
{
if(_presenter.CurrentUser.IsModified)
{
MessageBox.Show("Nie zapisales zmian!");
e.Cancel = true;
}
}
And I got stackOverflow ;) Exception. Maybe someone now what is wron with this code?
You're recursively calling the method in itself forever. There's no terminating condition for these recursive calls. It'll result in Stack Overflow.
protected override void OnSelectedIndexChanged(EventArgs e)
{
CancelEventArgs cArgs = new CancelEventArgs();
OnSelectedIndexChanged(cArgs); // Clearly calling yourself indefinitely.
//...
}
protected override void OnSelectedIndexChanged(EventArgs e)
{
CancelEventArgs cArgs = new CancelEventArgs();
//Next line!!
OnSelectedIndexChanged(cArgs);
if(!cArgs.Cancel)
{
base.OnSelectedIndexChanged(e);
}
}
You're calling yourself. Hence the StackOVerflow exception.
You have OnSelectedIndexChanged call within OnSelectedIndexChanged, it's an endless recursive call.

Categories