this doesn't work
protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (#Session["PaginaAnterior"] == #"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx")
Response.Redirect("~/paginas/anunciar/carro/identificacao.aspx");
}
this on the other hand works
protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (#"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx" == #"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx")
Response.Redirect("~/paginas/anunciar/carro/identificacao.aspx");
}
this also doesn't work myGlobalVariable has the same values as the string it compares
protected void ods_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
if (#myGlobalVariable == #"http://www.francawdesign.com.br/veiculos/paginas/anunciar/carro/identificacao.aspx")
Response.Redirect("~/paginas/anunciar/carro/identificacao.aspx");
}
since a long time I have this doubt. My guess is that this features are not available at the time of this event. Is that correct?
Related
i++ keep on increasing whenever I reload the page.It should only increment when I trigger the button but I found out that during page reload it also increments.
I did the !IsPostBack but I still encounter the problem.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number();
}
}
private static int i;
private void cart_number()
{
lbl_cart_number.Text = i++.ToString();
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number();
}
When you reload a page, it means that it's not a IsPostBack. You should remove cart_number(); from your Page_Load. The Page_Load will be triggered each time there is an interaction between the browser and the webserver.
Remove cart_number() method call from your 'Page_Load'. No need to call that method on Page_Load. Any specific reason why you call from Page_Load()?
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
cart_number("1");
}
}
private static int i;
private void cart_number(string flag)
{
int lbl=0;
lbl =int.Parse(lbl_cart_number.Text);
if(flag!="1"){
i=lbl;
if(i>=0){
lbl_cart_number.Text =( i+1).ToString();
}
}
else
{
lbl_cart_number.Text ="0";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
cart_number("2");
}
I would like to call btnSubmit if certain conditions in axTws1_tickPrice are true. How do i do this?
private void btnSubmit_Click(object sender, EventArgs e)
{
//code here
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
Call butSubmit (how do i do this)
}
}
You're better off having a common method that both of your control handlers call, rather than trying to call one handler from another. That way your code is more extensible and testable, and you don't have to worry about the event arguments or senders.
For example:
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
Multiple options.
Option 1 :
Preferred approach, move common logic to another method.
private void btnSubmit_Click(object sender, EventArgs e)
{
CommonLogic();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
CommonLogic();
}
}
private void CommonLogic()
{
// code for common logic
}
Option 2:
Executing PerformClick() method which generates a Click event for a button.
btnSubmit.PerformClick();
Option 3:
Invoke the event method like any other normal method.
btnSubmit_Click(sender, new EventArgs());
just call it with current parameters.
if (Condition)
{
butSubmit(sender, null)
}
Unbelievable, but
btnSubmit_Click(null,null);
Or other arguments if needed.
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
button1_Click(sender, EventArgs.Empty);
}
}
button1_Click is similar to a normal method which accepts two inputs of type object and EventArgs So you can call them by giving the same parameters. if you are not going to use these arguments inside the method then you can call them by passing null,null don't use null if you want to use e or sender inside the method. in such situation call them as like i suggested above.
Thanks Steve and Hari - this worked well
private void btnSubmit_Click(object sender, EventArgs e)
{
DoStuff();
}
private void axTws1_tickPrice(object sender, AxTWSLib._DTwsEvents_tickPriceEvent e)
{
if (Condition)
{
DoStuff();
}
}
private void DoStuff()
{
// code to do stuff common to both handlers
}
I'm developing a calendar for ASP.NET. I'm not using the Calendar control because it's quite limited.
I was wondering how can I programmatically switch between different months, for example, show a previous and a next month?
Now I get to change a month only once and then the month gets stuck: if July is shown first, then I can only get to June. When I'm on June and push the next month button, it shows me August. Would AJAX be a good choice to solve this problem?
My code:
private static DateTime now = DateTime.Today;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPrev_Click(object sender, EventArgs e)
{
lblDateCal.Text = now.AddMonths(-1).ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
lblDateCal.Text = now.AddMonths(+1).ToString("MMMM");
}
Each time, you are using Now() to increment or decrement the month by one. You need to save your current month you are navigating to. E.g., save the last date you navigated to in the ViewState, and use that in your click events instead of Now().
for example:
protected DateTime UpdateDate(int offset)
{
DateTime dt;
if (ViewState["LastDate"] == null)
dt = DateTime.Now.AddMonths(offset);
else
dt = ((DateTime)ViewState["LastDate"]).AddMonths(offset);
ViewState["LastDate"] = dt;
return dt;
}
protected void btnPrev_Click(object sender, EventArgs e)
{
lblDateCal.Text = UpdateDate(-1).ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
lblDateCal.Text = UpdateDate(1).ToString("MMMM");
}
otherwise, if you prefer to use a static variable, then you need to utilize your static variable properly, by setting it each click. That is to say, the AddMonths() method does not implicitly modify your variable.
e.g.
protected void btnPrev_Click(object sender, EventArgs e)
{
now = now.AddMonths(-1);
lblDateCal.Text = now.ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
now = now.AddMonths(+1);
lblDateCal.Text = now.ToString("MMMM");
}
However, since static variables are global to the application, I would not think is the best approach.
Here is a good thread on that here: static variables in asp.net/C#
The reason its happening because you are using a static DateTime variable
please try this way
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnPrev_Click(object sender, EventArgs e)
{
lblDateCal.Text = DateTime.Today.AddMonths(-1).ToString("MMMM");
}
protected void btnNext_Click(object sender, EventArgs e)
{
lblDateCal.Text = DateTime.Today.AddMonths(+1).ToString("MMMM");
}
BreakPoint not working in Init, InitComplate, PreLoad events in ASP.NET page with C# in VS2008. But it is working for Page_Load event.
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void InitializeComponent()
{
this.PreLoad += new System.EventHandler(this._Default_PreLoad);
this.InitComplete += new System.EventHandler(this._Default_InitComplete);
this.Init += new System.EventHandler(this._Default_Init);
this.PreRender += new System.EventHandler(this._Default_PreRender);
this.PreInit += new System.EventHandler(this._Default_PreInit);
this.SaveStateComplete += new System.EventHandler(this._Default_SaveStateComplete);
}
protected void _Default_InitComplete(object sender, EventArgs e)
{
........
}
protected void _Default_Init(object sender, EventArgs e)
{
.........
}
protected void _Default_PreLoad(object sender, EventArgs e)
{
..........
}
}
EDIT: move your handlers adding into the OnInit instead of InitializeComponent method:
override protected void OnInit(EventArgs e)
{
// move your initializers here
}
But actually you don't need these initializers at all because of all these handlers could be hooked automatically with AutoEventWireUp=true e.g.:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_PreLoad(object sender, EventArgs e)
{
.........
}
protected void Page_InitComplete(object sender, EventArgs e)
{
........
}
protected void Page_Init(object sender, EventArgs e)
{
.........
}
protected void Page_PreRender(object sender, EventArgs e)
{
.........
}
protected void Page_SaveStateComplete(object sender, EventArgs e)
{
.........
}
}
EDIT II: As far as I remember, InitializeComponent is for VS 2003, .NET v1.1. Back then, the InitializeComponent was a place where the IDE serialized construction of the WebForm.
Now this method get never called from your code, so there are no event handlers you expected (and supposed to be added). Now there are 2 options to add hanlers: with AutoEventWireUp=true for general Page events and e.g. in overrided OnInit method.
Try this to help solve your problem of some breakpoints working, some not:
In the Debug menu, select "Delete All Breakpoints"
Save your solution and close Visual Studio
Open your solution, and re-establish your breakpoints in your events.
This should ensure that your breakpoints are set and hit properly.
I want to call btnDisconnect_Click within btnExit_Click.
private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
}
Usually in cases like these I make my click handlers only call another function and pass in appropriate arguments:
private void btnDisconnect_Click(object sender, EventArgs e)
{
DoDisconnect();
}
private void DoDisconnect()
{
...
}
Then I can call that same function from wherever:
private void btnExit_Click(object sender, EventArgs e)
{
DoDisconnect();
}
This way your "disconnect" logic is gummed up by taking dummy arguments that don't actually affect the disconnect behavior in any way.
It also means you can start factoring out view logic from forms.
That depends on if you are using the arguments passed to the event handlers
You could yust call it using nulls
Something like
private void btnDisconnect_Click(object sender, EventArgs e)
{
//does something
}
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
btnDisconnect_Click(null,null);
}
They're just methods. Just call it. You'll need to provide whatever event arguments btnDisconnect_Click is expecting (which is probably nothing). So the simplest thing is:
private void btnExit_Click(object sender, EventArgs e)
{
btnDisconnect_Click(this, EventArgs.Empty);
}
This will pass the current form/window/whatever it is as the sender, and an EventArgs object with no data.
You can call it just as you have it listed. The this below isn't necessary but it puts context on the code:
private void btnExit_Click(object sender, EventArgs e)
{
//I want to call btnDisconnect_Click. What line of code should I use here?
this.btnDisconnect_Click(null, null);
// If you need to have sender as something you can always put
// this in directly
this.btnDisconnect_Click(this.btnDisconnect, new System.EventArgs());
}
I'm going to make an assumption here and say that what you're trying to do is call a Disconnect (perhaps a network resource) for both the disconnect and exit buttons. Instead of calling one event handler method from the other you may want to refactor the disconnect event handler's code into a separate method. Then call that method from both handlers. For example:
private void Disconnect()
{
//Disconnect here
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
//do some other stuff here
Disconnect();
}
private void btnExit_Click(object sender, EventArgs e)
{
//do some other exit stuff here
Disconnect();
}
This makes your code much cleaner and saves you from having to call one event handler from another. This begins to separate your view logic from the rest of your program's logic, which is much more desirable and much easier to maintain in the long run. For instance you may want a separate controller for handling the network resource, instead of embedding it into the view's logic.
In the simplest case you can just call the btnDiconnect_Click directly as follows:
private void btnDisconnct_Click(Object sender, EventArgs e)
{
//Does Something
}
private void btnExit_Click(Object sender, EventArgs e)
{
//Call btnDisconnect_Click()
btnDisconnect_Click(sender, e);
}
You could just call the method passing in valid parameters.
btnDisconnect_Click(btnDisconnect,new EventArgs());
However you might want to consider refactoring out the code in btnDisconnect into a new method and calling that instead:
private void doSomething()
{
//....
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
doSomething();
}
private void btnExit_Click(object sender, EventArgs e)
{
doSomething();
}
{// this is probably your constructor
.
public delegate void MyCustomHandler(object sender, EventArgs e);
.
MyCustomHandler myCustomHandler = new MyCustomHandler(); //you can do more in your delegates constructor, members etc
myCustomHandler += btnExit_Click;
myCustomHandler += btnDisconnect_Click;
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
// do Something
}
private void btnExit_Click(object sender, EventArgs e)
{
// do Something
}
//And wherever you need to invoke these, you do
myCustomHandler(object sender, EventArgs e);