I have this page say test.aspx.Its codebehind is like the code below.
protected void Page_Load(object sender, EventArgs e)
{
}
public void display()
{
// some block statements
}
Since the function display() is outside the Page_Load it is never called. So how do
I make this call to this function after Page_Load.
NOTE: I need this function outside the Page_Load.
Use Page.LoadComplete:
protected void Page_LoadComplete(object sender, EventArgs e)
{
display();
}
protected void Page_Load(object sender, EventArgs e)
{
//call your function
display();
}
protected void Page_PreRender(object sender, EventArgs e)
{
//call your function even later in the page life cycle
display();
}
public void display()
{
// some block statements
}
Here is the documentation that discusses the various Page Life Cycle methods: http://msdn.microsoft.com/en-us/library/ms178472(v=vs.90).aspx
After Page_Load you have:
PreRender
Render
Unload
If you want to load that function on the time of load only then do like this
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{
display();
}
}
public void display()
{
// some block statements
}
as this will load only once. But if u want to load it on each post back then do like this
protected void Page_Load(object sender, EventArgs e)
{
if(!isPostBack)
{}
dispplay();
}
public void display()
{
// some block statements
}
There's a default event called Page_LoadComplete which will execute when page is loaded fully
But,If you write your code in a Page_Load Event that code will execute and your controls will be accessible there
So best to call like in a page_load for once with first postback ;)
But, still if you want to go after page load then go for the Page_LoadComplete
protected void Page_LoadComplete(object sender, EventArgs e)
{
display();
}
public void display()
{
// some block statements
}
Related
In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }
I would like to link the function of my Chart Output Button to the items in my check box list such that when an item in the check box is selected or deselected, the page is reloaded again and my chart is refreshed.
My codes to refresh the chart have already been included for the Chart Output Button.
protected void ChartOutputButton_Click(object sender, EventArgs e)
{ //do something }
protected void CheckBoxList1_SelectedIndexhanged(object sender, EventArgs e)
{ // display ChartOutputButton method here }
Thanks.
You can directly call button event handler which generates the chart:
protected void ChartOutputButton_Click(object sender, EventArgs e)
{
//your code to generate chart
}
protected void CheckBoxList1_SelectedIndexhanged(object sender, EventArgs e)
{
//you can call ChartOutputButton click event handler here
ChartOutputButton_Click(ChartOutputButton, null);
}
Is this what you mean?
protected void ChartOutputButton_Click(object sender, EventArgs e)
{
chart_refresh();
}
private void chart_refresh()
{
// do somethiing
}
protected void CheckBoxList1_SelectedIndexhanged(object sender, EventArgs e)
{
// display ChartOutputButton method here
chart_refresh();
}
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 been trying to template control panels in my site so I can take a panel and populate it fully. I'm good up until the point where my event handling needs to access functions on my page. My current test will take me to a login redirect page. So how can I get this event handler to perform a redirect?
public class DebugButton : Button
{
public string msg;
public DebugButton()
{
this.Click += new System.EventHandler(this.Button1_Click);
this.ID = "txtdbgButton";
this.Text = "Click me!";
msg = "not set";
}
protected void Button1_Click(object sender, EventArgs e)
{
msg = "Event handler clicked";
}
}
*on the Page*
protected void Page_Load(object sender, EventArgs e)
DebugButton btnDebug = new DebugButton();
PnlMain.Controls.Add(btnDebug);
Really appreciate the help. Thanks!
To do a redirect you can use:
Note:
Assuming that your login page is named login.aspx and it is located in root folder of your website.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("~/login.aspx");
}
or
protected void Button1_Click(object sender, EventArgs e)
{
Server.Transfer("login.aspx");
}
If you want the event to have access to the page, then the page needs to subscribe to the click event.
Aka:
on the Page
protected void Page_Load(object sender, EventArgs e)
{
DebugButton btnDebug = new DebugButton();
btnDebug.Click += new System.EventHandler(Button1_Click);
PnlMain.Controls.Add(btnDebug);
}
protected void Button1_Click(object sender, EventArgs e)
{
// access whatever you want on the page here
}
I just found out that that System.Web.HttpContext.Current will get me the current context of the page. So long as the custom class is part of the app(this one is in the apps folder of course) I'm good to go. Heres a sample of my quick TestTemplate that I used to make a custom button.
public class TestTemplate : Button
{
public TestTemplate()
{
this.Text = "Click Me";
this.ID = "btnClickMe";
this.Click += new System.EventHandler(this.EventHandler);
//
// TODO: Add constructor logic here
//
}
public void EventHandler(object sender, EventArgs e)
{
//System.Web.HttpContext.Current.Server.Transfer("Default.aspx");
System.Web.HttpContext.Current.Response.Write("This is a test!");
}
}
I have contentpage overriding the OnInit event of a masterpage. The override works fine, until I put a custom user control on the page: in this case the OnInit event does not fire for the contentpage (no overrides are used in the user control)
What are the possible causes/solutions for this? (I use the OnInit event to create dynamic controls)
Edit:
now i tried this in the content page:
(The OnPreInit part runs, but Masters_Init does not get called...)
protected override void OnPreInit(EventArgs e)
{
base.Master.Init += new EventHandler(Masters_Init);
}
void Masters_Init(object sender, EventArgs e)
{
//code
}
Are you calling the base.OnInit?
public override void OnInit(EventArgs e)
{
// code before base oninit
base.OnInit(e);
// code after base oninit
}
Update
public class Page1 : Page
{
public Page1 : base() {
PreInit += Page_PreInit;
}
void Page_PreInit(object sender, EventArgs e)
{
Master.Init += Master_Init;
}
void Master_Init(object sender, EventArgs e)
{
//code
}
}
Also as mentioned in the comments I would recommend not overriding the events if you don't have to, but if you must be sure to call the base. so in your edit above it should be
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
base.Master.Init += new EventHandler(Masters_Init);
}