Web Forms :: How To Raise Event On Usercontrol From Onload Events
Jan 7, 2010
I have a login.ascx control with three controls (LoginName, Password and btnLogin) , I want to rase a click event on this button from .aspx page because some restriction is there so that I am unable to write any code on .ascx page.I am bale to put loginName and password on .ascx by using query string but unale to raise event.
I have a login.ascx control with three controls (LoginName, Password and A Button) , I want to rase a click event on this button from .aspx page because some restriction is there so that I am unable to write any code on .ascx page.
I have assigned value on login name and password from .aspx onload events. but I an facing problem while rasing event on button.
I am showing code that I have written on .aspx page that I have putten onload section.
I have user control that inherits a base control class and these user controls are loaded using the LoadControl method, I can't seem to figure out how to raise events from user controls to the page that are dynamically loaded this way. Here is the delegate and event in the base user control class.
public delegate void SomeChangeEventHandler(object sender , SomeChangeEventArgs e); public event SomeChangeEventHandler SomeChangeEvent; public virtual void OnSomeChanged(SomeChangeEventArgs e) { if (SomeChangeEvent != null) { SomeChangeEvent(this, e); } }
I'm working on a questionnaire type application in which questions are stored in a database. Therefore, I create my controls dynamically on every Page.OnLoad.This works like a charm and ViewState is persisted between postbacks because I ensure that my dynamic controls always have the same generated Control.ID.
In addition to the user control that dynamically populates the questions, my questionnaire page also contains a 'Status' section (also encapsulated by a user control) which represents the status of the questionnaire (choices are 'Complete', 'Started' or 'In Progress'). If the user changes the status of questionnaire (i.e. from 'In Progress' to 'Complete'), I need to postback to the server because the contents of the dynamic portion of the questionnaire depend on the selected status.
Some questions are always present regardless of status, and yet others may not be present at all for the selected status. The point is, when the status changes, I have to postback to the page and render the right set of questions. Additionally, I need to preserve any user entered values for those questions which are 'always available'.
However, due to the page life cycle in ASP.NET, the 'Status' user control's OnLoad, which contains the correct status needed to load the right questions from the DB, doesn't get executed until after the 'dynamic questions' user control has already been populated (with the wrong/stale values).
To get around this, I raise an event from my 'Status' user control to the main page to indicate that the Status has changed. The main page then raises an event on the 'dynamic questions' user control. Since by the time this event bubbles up, the 'dynamic questions' user control has already loaded the 'wrong' questions from the DB, it first calls Controls.Clear. It then happily uses the new status to query the database for the 'correct' questions and does a Control.Add() on each. FYI, Control.IDs are consistent across postbacks.
This solution works...sorta. The correct set of questions for the selected status do get rendered; however ViewState is getting lost for those 'always available' questions. I'm guessing this is because the 'dynamic questions' user control calls Controls.Clear when responding to the status changed event. This must somehow kill the association between ViewState and my dynamic controls, even though the Control.IDs are consistent.
This seems like such a common requirement, I'm virtually certain there is a better, cleaner and less error prone approach to accomplish this. In case its not plainly obvious, I haven't been able to grok the ASP.NET page life-cycle despite working with it for the last year.
i need when i select a image in fileupload control then path should store in a textbox. actually in gridview i have a image control ,one fileupload control and a textbox control. so when user trying to edit one image and click Edit button from gridview and chose a image from fileupload control then path should be store in relative textbox control.below is my design.
Found this question on an interview siteGiven the following methods of the ASP .Net Page class, in which of them would you attach an event handler to an event published by a control on the web page?
my dropdown list contains 3 items google,yahoo,hotmail.
It the user selects google msgbox should be raised saying "You hae selected google". Thats it. I m not getting how to do that. I tried on selectedchangeindex event but nothing happened.
I have the problem that when i´m adding my usercontrol to my default page the events do not fire. I already know that I had to add the usercontrol as soon as possible to the lifecycle, but I can´t because I know the number of buttons after clicking on my default button.
code:
my default.aspx.cs page protected override void OnInit(EventArgs e) { if (!IsPostBack) { addPruefstation aP = (addPruefstation)this.LoadControl("addPruefstation.ascx"); aP.ID = "ucPruefstation"; Session["ucPruefstation"] = aP; } base.OnInit(e); } protected void bApply_Click(object sender, ImageClickEventArgs e) { int anz; Int32.TryParse(((TextBox)tablePruefstand.FindControl("tbPruefstandstationen")).Text, out anz); //here i´m getting the number of buttons Session["anzStation"]=anz; addPruefstation aP = (addPruefstation)Session["ucPruefstation"]; aP.anz = anz; Session["ucPruefstation"] = aP; phPruefstation.Controls.Add(aP); } my usercontrol.acsx.cs: public int anz; protected override void OnInit(EventArgs e) { for (int i = 0; i < anz; i++) { tcButton=new TableCell(); trPruefstation = new TableRow(); tcEmpty = new TableCell(); tcLabel = new TableCell(); tcTextbox = new TableCell(); tcLabel.Controls.Add(new LiteralControl("Anzahl Leaks")); tcTextbox.Controls.Add(new TextBox() { ID = "Leak" + i, Text = "0", AutoPostBack = false }); ImageButton imgButton = new ImageButton() { ID = "" + i, ImageUrl = "~/Img/1rightarrow-256.png", Height=15}; imgButton.Click += new ImageClickEventHandler(imgButton_Click); tcButton.Controls.Add(imgButton); trPruefstation.Cells.Add(tcEmpty); trPruefstation.Cells.Add(tcLabel); trPruefstation.Cells.Add(tcTextbox); trPruefstation.Cells.Add(tcButton); tablePruefstation.Rows.Add(trPruefstation); } base.OnInit(e); } void imgButton_Click(object sender, ImageClickEventArgs e) //this event never be fired { throw new NotImplementedException(); }
Here I have gridview control with checkbox for selection of one or two records. Have one button and when clicking this , i need to get the selected records values and assign this values to the textbox which is in usercontrol1.
I want to know how to raise an event for fileupload control. In my project, as soon as I select a file(Image) it should show FILENAME,EXTENSION & SIZE in the labels given below.
How to raise combobox_SelectedIndexChanged(object sender, EventArgs e) programmatically? Lets say i have method called ClearFields(). I want to call the event before i hit the end of Clearfields() method.
for my dynamic user control, i am trying to do any event from that control (delete, update .... etc) but it disappeard when i make any event! this the code in the page to load it dynamically:
I have a usercontrol (ascx) with a DropDownList placed on a MasterPage. It has AutoPostBack=true. I created a SelectedIndexChanged event for the dropdown on the usercontrol, but it does not fire when seleted item changes. The PostBack fires and both current page as well as MasterPage are reloaded but the SelectedIndexChanged is not fired. How can this event be trapped?
I have a user control which I load dynamically using LoadControl. In this UserControl I have a button with assigned OnClick event, but when I click that button, the event is not fired. I'm populating the user control in UpdatePanel's Load event (I invoke __dopostback from JS).
I need to generate a gridview with dynamic columns, on each cell I need a button that inserts-deletes a relationship in a database. The relationship is of course between what is represented by each column and row. I decided to use a gridview and create templatefields which I add programmatically to the table. I successfully got to that point but when i decided to raise a click event from the buttons or a rowcommand from the gridview I could not do it. I´m looking into doing this with button fields right now but I´m curious on how this can be accomplished?
I have a simple Tabcontainer with 3 tabpanels. I want to load some content to the different tabs the first time they are clicked (selecting the tabs). I have tried using Protected Sub TabContainer1_ActiveTabChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabContainer1.ActiveTabChanged but that only works, WHEN there has been changes to the content. So my question is: How do I raise a server side event like loading a droplist when the tab is clicked?
I'm working on custom web control, inherited form label control. Try to run some JavaScript in page load event. But not working! Or any equivalent onload event for label control?
I have a LinkButton inside a GridView (via an TemplateField). No matter what I try, the LinkButton will not invoke its event handler. I have tried both traditional event handler ("OnClick")A OnRowCommand event handler at the GridView level.In both cases, I've debugged and it doesn't even catch the event handler.