In my application I'm using a usercontrol for two pages: AddInfo.aspx and EditInfo.aspx.
The thing is that I want different thing to happen when saving the info, depending on which page the user is at (ie. what he's actually doing).
So what i'm wondering is if there's any way to use an if statement to find out which page that right now is using the user control? In that case my problem could be solved.
protected void SaveButton_Click(object sender, EventArgs e) {
if (//The page using the usercontrol = Edit.aspx) {
// do this...
}
else {
// do that...
}
}
Thanks in advance
protected void SaveButton_Click(object sender, EventArgs e) {
if (this.Page is EditInfo) {
// do this...
}
else {
// do that...
}
}
Where EditInfo is your page's class.
You could also define a Behavior property on your user control and set it in your Xaml code according to what you want in which page. This would be a nice way to avoid the need to know where you are.
protected void SaveButton_Click(object sender, EventArgs e) {
if (Request.Url.AbsoluteUri.Contains("Edit.aspx")) {
// do this...
}
else {
// do that...
}
}
Related
Lets assume I have this code
private void Exitbtn_Click(object sender, EventArgs e)
{
Application.Exit();
}
And I want to call this function from another part of my code and not only from the click of the user
code...
{
...do something...
Exitbtn_Click()
}
I have tried Exitbtn.Click +=new EventHandler(Exitbtn_Click); but it doesn't work i believe it is because of the word new but I am not sure, I am new to C#
Whilst you could call:
Exitbtn_Click(null, EventArgs.Empty);
If you actually need sender / EventArgs to be populated it could become messy.
Personally it seems cleaner to wrap the common logic in a private method and call from both places.
private void ApplicationExit()
{
Application.Exit();
}
private void Exitbtn_Click(object sender, EventArgs e)
{
ApplicationExit();
}
code...
{
...do something...
ApplicationExit();
}
As john said in the comments:
Exitbtn_Click(null, EventArgs.Empty);
I am trying to figure out how to make it that when my timer ticks, it performs a bidder00_TextChanged, or something like that.
Is this even possible to do? and if it isn't, is there any other way to do it?
I tried to search Google for it but i didn't get any results, if you find anything that i missed please post it here.
I don't really have any code but here it is:
private void bidder00_TextChanged(object sender, EventArgs e)
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
That is my TextChanged Event
My timer doesn't have any code because it is going to perform the bidder00_TextChanged Event.
You could create a method Perform() and call it from within your event handlers :
private void timer1_Tick(object sender, EventArgs e)
{
Perform();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
Perform();
}
private void Perform()
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
I assume you have coupled your actual logic with your click event which is not a good idea. Separate the code out into a separate function and have both parts of the application call the same code e.g.
private void SubmitBid()
{
// code you want to execute
}
private void OnSubmitBid()
{
// confirm whether we can actually submit the bid
if (bidder00.Text == addbidder1.Text)
{
SubmitBid();
}
}
private void Timer1_OnTick(object sender, EventArgs e)
{
// trigger code from timer
OnSubmitBid();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
// trigger code from text change
OnSubmitBid();
}
private void btnBid_Click(object sender, EventArgs e)
{
// trigger code from button press
OnSubmitBid();
}
Notice all the UI controls trigger the same code. There is an extra call in there for the text control validation (i.e. OnSubmitBid()) - if this wasn't required then you would just call SubmitBid directly.
I've got these Methods:
private void button_Click(object sender, EventArgs e)
{
//Changes the Text in the RichBox, EXAMPLE:
richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
}
And,
private void richTextBox_TextChanged(object sender, EventArgs e)
{
//Wants something like that
if(called from button_click)
{
//DO SOMETHING
}
else
{
//DO SOMETHING
}
}
How can I handle this, to know if it was called from the Button_click?
Do I have to use the object sender to get informations? But how?
Hope u guys can help me
Just use a flag:
private bool _isInButtonClick;
private void button_Click(object sender, EventArgs e)
{
try
{
_isInButtonClick = true;
//Changes the Text in the RichBox, EXAMPLE:
richtTextBox.Text = "Now Changed and calling Method richTextBox_TextChanged";
}
finally
{
_isInButtonClick = false;
}
}
private void richTextBox_TextChanged(object sender, EventArgs e)
{
if(_isInButtonClick)
{
//DO SOMETHING
}
else
{
//DO SOMETHING
}
}
private void richTextBox_TextChanged(object sender, EventArgs e)
Here sender is the richTextBox, not the button that changed the text.
You could go into the stack trace to discover if the button click is on the call stack, but that's overkill (like using a nuke to crack a walnut).
Add a flag (bool) to your form, set it to true in the button click, and check it in the TextChanged event, then at the end of the button click, set it to false again.
If you do this I would advise wrapping this signal logic in a class that implements IDispose and use it in using statements.
That said, are you sure you need this functionality?
I have created a composite control with sample details as follows. Basically, the first time on page load the control sets a view state variable and the problem is that on post back (when the button is clicked), the ViewState variable is null. I have researched extensively and I am not able to find a solution. I checked all the Microsoft recommended articles and also from other developers. This approach seem to work for everyone and I can't figure out what I'm doing wrong. If anyone can help, I would really appreciate it.
PS: This code may not work as it is only for illustrative purposes. but this is exactly what i'm doing in my code.
Public class Test : CompositeControl
{
private Button btnTest = new Button();
public string TestViewState
{
get
{
string s = (string)ViewState["test"];
return (s == null) ? String.Empty : s;
}
set
{
ViewState["test"] = value;
}
}
private void set()
{
TestViewState = "test";
}
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
set();
}
protected override void RecreateChildControls()
{
EnsureChildControls();
}
protected override void CreateChildControls()
{
base.Controls.Clear();
btnTest.ID = "btnTest";
btnTest.Click += new EventHandler(btnSubmitTest_Click);
if (!ChildControlsCreated)
Controls.Add(btnTest);
base.CreateChildControls();
}
protected override void Render(HtmlTextWriter writer)
{
btnSumbit.Render(writer);
}
protected void btnSubmitTest_Click(object sender, EventArgs e)
{
string test = TestViewState; // Viewstate value is null here!!!!!!
}
}
Are you sure that Page_Load is getting called? As far as I can remember that "notation" works only on pages and User Controls (didn't check that). Try overriding:
protected override void OnLoad(EventArgs e)
{
...
}
Test it with a Debugger.
Ok, the enableviewstate was disabled at the web.config level by another team member. Glad I found it. Thanks Arthur for confirming it worked for you.
I am building a Asp.net Application. I need to save a HashTable in a session.
At page load i am writing
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Session["AttemptCount"]=new Hashtable(); //Because of this line.
}
}
Here problem is, when a user refresh the page, session["AttemptCount"] also get refreshed.
I want to know where should I declare
Session["AttemptCount"]=new Hashtable();
So that my seesion do not get refeshed.
EDIT In Global.asax, this session will get started, as soon as user opens the website. I want to creat this session only if user go to a particular page. i.e Login.aspx
Do it in the Session_Start method in your Global.asax like so...
protected void Session_Start(object sender, EventArgs e)
{
Session["AttemptCount"]=new Hashtable();
}
Update:
Then simply just do a check to see if the session variable exists, if it doesn't only then create the variable. You could stick it in a property to make things cleaner like so...
public Hashtable AttemptCount
{
get
{
if (Session["AttemptCount"] == null)
Session["AttemptCount"]=new Hashtable();
return Session["AttemptCount"];
}
}
And then you could just call on the property AttemptCount wherever you need like so...
public void doEvent(object sender, EventArgs e)
{
AttemptCount.Add("Key1", "Value1");
}
You could make a property like this in your page:
protected Hashtable AttemptCount
{
get
{
if (Session["AttemptCount"] == null)
Session["AttemptCount"] = new Hashtable();
return Session["AttemptCount"] as Hashtable;
}
}
then you can use it without having to worry:
protected void Page_Load(object sender, EventArgs e)
{
this.AttemptCount.Add("key", "value");
}
test if it exists first
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if(Session["AttemptCount"] == null)
{
Session["AttemptCount"]=new Hashtable(); //Because of this line.
}
}
}
though the session_start is better, you only need to uses it on one page but you can create it for each session.
Hashtable hastable_name=new Hashtable()
Session["AttemptCount"]=hastable_name
Look at Global.asax and the Application_Started (I think) and there is one for session started too.