I knew about Page PreRender event and also about overriding OnPreRender, but didn't know existence of Page_PreRender method. Recently while going through this forum i found and search the difference between both but couldn't get much?
Here is my observation:
I have a page with label Label1.
Scenario 1:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "load ";
}
protected override void OnPreRender(EventArgs e)
{
Label1.Text += "OnPreRender ";
base.OnPreRender(e);
}
private void Page_PreRender(object sender, System.EventArgs e)
{
Label1.Text += "Page_PreRender ";
}
value of Lable1 would be: load OnPreRender Page_PreRender
Scenario 2:
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = "load ";
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
Label1.Text += "OnPreRender ";
}
private void Page_PreRender(object sender, System.EventArgs e)
{
Label1.Text += "Page_PreRender ";
}
Note the call order of base.OnPreRender(e);
value of Lable1 would be: load Page_PreRender OnPreRender
I can only say base.OnPreRender(e) is calling Page_PreRender(), but isn't it private?
Can some one please tell, what is difference between both and when to use which?
In general which one should be used?
The OnPreRender method of the page is responsible for raising the Page_PreRender event.
Usually you should just subscribe to the Page_PreRender event.
If you need to do some special processing before it's safe to raise the Page_PreRender event, then you can override the OnPreRender method and do the processing before calling base.OnPreRender(e).
The Page_ events are called when AutoEventWireup="true" is set for your page. This is how you can create them and have them called without being explicitly wired up.
Related
Is there a way that I force all TextBoxes (as an example) .. on an asp.net form, to go through the same routine, during a "postback" event or onchange event?
Here is example code.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
textBox1.TextChanged += new EventHandler(textBox_TextChanged);
textBox2.TextChanged += new EventHandler(textBox_TextChanged);
}
}
protected void textBox_TextChanged(object sender, EventArgs e)
{
}
I'm trying to set an initial selection for a DropDownList by calling: drop.SelectedIndex = 5 in Page_Load.
This works, but then if I change the selection manually and want to save the form, I'm still getting the initial selection instead of the new selection when calling drop.SelectedValue. What's wrong?
You have forgotten you check if(!IsPostback). Otherwise you will select the 6th item again on postbacks before the SelectedIndexChanged event is triggered (or a button-click event):
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack) // do this only on the initial load and not on postbacks
dropDwonList1.SelectedIndex = 5;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//set up data here
}
}
if (Page.IsPostBack)
{
//do page reload logic in here
}
protected void foo(object sender, EventArgs e)
{
//get your selected value here
}
Try this code
You should be using if(!IsPostback) in the Page_Load function.
protected void Page_Load(Object sender, EventArgs e)
{
if(!IsPostBack)
{
drop.SelectedIndex = 5;
//yourcode
}
}
Through this your problem will be solved
I have a drop down box that populates textbox values based on its value. It fires when it is changed but on pageload it doesnt fire. How do I get it to fire on page load?
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox3.Text = DropDownList1.SelectedValue;
TextBox12.Text = DropDownList1.SelectedValue;
TextBox21.Text = DropDownList1.SelectedValue;
//etc
Tim Schmelter's comment is right on the money.
// Wire up to the page load event
protected void Page_Load(object sender, System.EventArgs e)
{
updateTextBoxes();
}
// Wire up to the select index-changed event
protected void DropDownList1_SelectIndexChanged( object sender, EventArgs e )
{
updateTextBoxes();
}
// your workhorse method
protected void updateTextBoxes()
{
TextBox3.Text = DropDownList1.SelectedValue;
TextBox12.Text = DropDownList1.SelectedValue;
TextBox21.Text = DropDownList1.SelectedValue;
// etc.
}
It won't be called automatically at page load, you have to call it "manually":
void Page_Load(object sender, System.EventArgs e) {
// ....
DropDownList1_SelectedIndexChanged(DropDownList1, e);
}
SelectedIndexChanged fires in response to a user-driven change. Move the assignment logic to another method and call it manually from Page_Load and also from your event handler.
This is what I'm trying:
public partial class _Default : System.Web.UI.Page
{
String test = "hi ";
protected void Page_Load(object sender, EventArgs e)
{
test = test + test;
Button1.Value = test;
}
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Value = "u're trolled !";
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
when the page is first loaded it shows "hi hi " as the Button1's value. But whenever I click Button2, it shows "u're trolled !" as Button1's value. My question is if the page is postback every time I click the button and the Page_Load is called, then why does it shows "u're trolled !" instead of appending "hi" ?? Isn't the Page_Load called every time the page reloads ?
Use this code to know which value come when :
String test = "hi ";
protected void Page_Load(object sender, EventArgs e)
{
test = test + test;
Button1.Value += test;
}
protected void Button2_Click(object sender, EventArgs e)
{
Button1.Value += "u're trolled !";
}
protected void Button1_Click(object sender, EventArgs e)
{
}
You need to understand the ASP.NET Page Life Cycle, as Code addict said its the sequence of events that causing the issue for you.
Following link will be very helpful for you.
http://msdn.microsoft.com/en-us/library/ms178472.aspx
In my application, i am trying to get button_click event in another button click event, but it is not working.
My code is,
protected void btn1_Click(object sender, EventArgs e)
{
Response.Write("hi.....");
}
protected void btn2_Click(object sender, EventArgs e)
{
btn1.Click += new EventHandler(this.btn1_Click);
}
What is wrong with this?
I suppose you want to call the code of btn1_click event.
If this is the case, you simple call it as a method in your btn2_Click.
protected void btn2_Click(object sender, EventArgs e)
{
btn1_Click (sender, e);
}