A page containts custom address control and checkBox. Why does the second example of code work properly, but first doesn't?
//1
protected void Page_Init(object sender, EventArgs e)
{
//doesn't work properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
//2
protected void Page_Load(object sender, EventArgs e)
{
//works properly
ucLegalAddress.Visible = !chkLegalAddress.Checked;
}
Because the viewstate of the controls is loaded between the init and the load event. This means that the init event does not know the state of the client yet.
MSDN lifecycle overview
Because all controls are create in OnInit() method, that call between Page_Init and Page_Load. In Page_Init all controls are null. Read more
Related
How can I call a gridview_SelectedIndexChanged event on page load?
Please guide me I'm using asp.net c#
Is this possible to use trigger method on page load?
You can:
protected void Page_Load(object sender, EventArgs e)
{
gridview_SelectedIndexChanged(gridview, null);
}
I have created a User Control(Popupcontrol) and in that control i have created a property(PageType) and when i am using the Popupcontrol on the page then i set the property(pagetype) according to the page.
but now there is some problem i have to two button on the page and on the second button click i want to change the pagetype property .So is there any solution for the same.
Based on your comment, it seems you bind the data (PageType property in your question) in the Page_Load event, instead of this it should be done in overrided DataBind method which should be called if the page is not in post back request (otherwise your data will be overwritting in the next Page_Load event as you mentioned in your comments):
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
DataBind();
}
}
public override void DataBind()
{
PageType = someValue;
}
after this your click handler may looks like:
protected void button2_Clicked(object sender, EventArgs e)
{
PageType = someOtherValue;
}
Are you setting the variable in a page load event? You may need to add:
if (!Page.IsPostback) {
// Code here.
}
I have a databound dropdown list (ASP.net). I want the page to load with a certain item as the selected item.
I am not adding a blank first row (thats not what i need)
I find that I can get this to work with "AppendDataBoundItems" to true, but the side-effect is that I have all the items listed twice.
thanks for your help!
Use the Page_PreRender event to handle this situation....
In the Page_Load register an event handler for the PreRender event
protected void Page_Load(object sender, EventArgs e)
{
Page.PreRender += new EventHandler(Page_PreRender);
}
And in the PreRender event,
void Page_PreRender(object sender, EventArgs e)
{
ComboBoxSomething.SelectedValue = WhatEverYouWant;
}
I need to insert a bit of Javascript into the process when a Web Form is submitted, but after the client side validation takes place.
RegisterOnSubmitStatement seems to place the javascript before the validation.
Anyone know how to get it to render after?
Solution found:
In a web control, I put something like this:
protected override OnInit(EventArgs e) {
Page.SaveStateComplete += new EventHandler(RegisterSaveStuff);
base.OnInit(e);
}
void RegisterSaveStuff(object sender, EventArgs e) {
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
that´s right, the RegisterOnSubmitStatement DO NOT WORK in the init function.
It should be called after in the page lige cycle.
I thing the right place therefor is:
"PreRenderComplete"
protected override OnInit(EventArgs e)
{
Page.PreRenderComplete+= new EventHandler(Page_PreRenderComplete);
base.OnInit(e);
}
void Page_PreRenderComplete(object sender, EventArgs e)
{
Page.ClientScript.RegisterOnSubmitStatement(typeof(Page), "name", "JS code here");
}
After some research online and playing around with it, I figured out that you can do it by hooking into the Page's SaveStateComplete event. I guess the validation submit statement is registered during the PreRender event, so if you register it after that (in the SaveStateComplete event) you can get it afterward.
You do have to re-register it, but that's not a big deal because I'm not relying on ViewState for my JS.
I have a user control inside of a repeater. The user control has an ImageButton, which when clicked, should raise an event to the page which will handle the event:
//Button onClick event in user control
protected void btnOpenOption_Click(object sender, ImageClickEventArgs e)
{
RaiseBubbleEvent(sender, e);
}
The following are two methods on the page. One to handle a BubbleEvent from a child control, the other to handle the repeater's ItemEvent command:
protected void rptProcessOptions_ItemCommand(object source, RepeaterCommandEventArgs e)
{
//do something...
}
protected override bool OnBubbleEvent(object source, EventArgs args)
{
//do something else...
}
I've read that the repeater ItemCommand handler should listen for the BubbleEvent from the child control and subsequently handle it, but it's not. The OnBubbleEvent handler on the page is not picking it up either. In other words, the event is just getting lost. I know it's firing because I can see that when I step through in the debugger.
I've used RaiseBubbleEvent before successfully, but never inside a repeater, so I'm not sure if what I'm attempting is correct. Any thoughts?
ItemCommand is only fired if the EventArgs is an instance of RepeaterCommandEventArgs.