I need to get textbox that exits in Formview inserttemplate via javascript.
Both return null:
$get('txtTitle');
document.getElementById("txtTitle");
The problem is that formview is not rendered on form load...
As you stated, the formview contents are rendered on the server upon request rather than on page load. That said, try this code to access controls in the formview. Change the name of 'FormView' to match your unique control ID.
document.getElementById('<%=FormView.FindControl("txtTitle").ClientID%>');
If needed, Here are a few useful events you can use to register the javascript in the code behind if there are lifecycle considerations.
The ItemCreated event is raised after all rows are created in a FormView control. This can occur when the control is first rendered, or when the user navigates to another record. You can use this event to provide an event-handling method that performs a custom routine, such as adding to or modifying the contents of a row, whenever this event occurs.
Note:
The ItemCreated event occurs before the FormView control is bound to data. To modify the value of a bound field, use the DataBound event.
Related
The scenario:
I have some JSON data which I'm using to load stored-data into fields on my form. One of these fields is a DropDownList. The DropDownList happens to be in a child, of a child ASCX control, which I'm accessing from a parent ASPX page. When this DropDownList has its SelectedIndexChanged, it makes other fields visible on the form.
I'm using one of my functions to find this control, which is working successfully, but when setting the SelectedValue of my DropDownList control, the SelectedIndexChanged event is not firing. Meaning some fields aren't loading, resulting in some JSON data not being loaded and lost.
I have seen a suggestion of simply calling ddl_SelectedIndexChange(sender, args) function, but the page I'm calling dynamically loads hundreds of child controls depending on the current request, so was wondering if there is a way of invoking the SelectedIndexChanged event (if it exists) for a control, without having to search and manually call the ddl_SelectedIndexChanged() function. Is it possible?
DirectCast(WebUtils.ControlFinder(upMain, f.fieldClientID), DropDownList).SelectedValue = f.fieldData.ToUpper()
I hope it makes sense. Sorry if I haven't made this clear enough.
I ended up using Reflection to Invoke the properties event that I required, worked a treat.
I am trying to take advantage of the page lifecycle in Asp.NET so that I don't bind to my datasources more than I actually need to.
This has led to the following problem. I have an ObjectDataSource, GridView, and a button on my page. The button adds a record to the database, which should be picked up by the data source and presented on the Grid. The problem is that the item is not showing up on the gridview until I refresh the page.
I can solve the problem by calling GridView.DataBind() in my button's event handler, but this goes against what I understand about the .NET Page Lifecycle.
Based on this article, the lifecycle should be as follows:
In addition the article states that the Databinding event is "Raised after the control's PreRender event, which occurs after the page's PreRender event."
So, my button click event should fire first, during the Event Handling phase. It should add the record the database.
Next PreRender should be called on the controls.
Finally DataBind should be called on the controls, and the Grid should update to capture the new record.
Yet this doesn't seem to happen. What am I not understanding?
I think the issue is that your viewstate is not enabled on your GridView. This is what I experienced, but then I also had to call DataBind() on the GridView from the page PreRender event if the request was a postback to get the data updated in the GridView on postback.
Edit:
It would help to understand the issue and context better if you could post the source code of your page (aspx + codebehind). How and where do you connect your GridView to your datasource? Statically in markup or dynamically? Do you make any calls to page.DataBind()? ... These things may influence the behaviour of the GridView.
I'm adding dynamic controls to a page based on data from a database to generate RadioButtonList questions.
Sometimes the questions are required, in which case after the data is retrieved from the database and the RadioButtonList is populated, I dynamically add a RequiredFieldValidator for the RadioButtonList.
The problem arises on postback, when in Page_Load() I execute Page.Validate() which always fails. I discovered that this is because the controls are being re-added on post-back but they aren't repopulated with the user's responses.
My question is:
When can I grab the user's response once they click "Submit", where do I store it, and what's the best way to bring it back so that Page.Validate() validates against controls with the proper responses?
Instead of creating the validation controls in Page_Load (or in a function that's called from within page_Load) do so in Page_Init
For more info see the Page Lifecycle:
In the Page Lifecycle, putting it in Page_Init allows the controls to be created on each page load, but BEFORE the viewstate values are applied. This means that the controls are created, and then the user selection is applied.
If you have it in Page_Load, then the controls are created AFTER the Viewstate values are applied. This means that the controls are just created from scratch after the viewstate has been applied, resetting everything to the default value.
Actually, this page says it better:
During page initialization, controls on the page are available and
each control's UniqueID property is set. Any themes are also applied
to the page. If the current request is a postback, the postback data
has not yet been loaded and control property values have not been
restored to the values from view state.
During load, if the current
request is a postback, control properties are loaded with information
recovered from view state and control state.
Use Page_Init when you
have to create controls dynamically. The controls are created every
time that the page is run. The best place to do this is in the
Page_Init function.
My ASP.NET form contains a collection of dynamically-created radiobuttons that are created and configured in the Page_Load event handler.
Normally, I process postback data in the Page_Load handler, using the condition:
if (IsPostBack)
However, since the controls that I need to access are created IN the Page_Load handler, the postback data from the previous rendering of the page is lost. To better illustrate the problem, here is an outline of the events as they occur:
1-Page_Load is invoked for the first time
2-An unknown number of radiobuttons are created dynamically
3-The radiobuttons are configured, based on information present on the server
4-The radiobuttons are added to the page's content
5-The user selects an option, and clicks the submit button
6-The Page_Load handler is invoked for the second time
7-The radio-buttons are added dynamically, exactly as before
8-The radio-button that the user checked is seemingly non-existant for processing
It seems like I need to be processing different parts of this in different event handlers. Is there an event that occurs after postback, but while the original radiobuttons are still accessible?
Check out the "Init" events...
http://msdn.microsoft.com/en-us/library/ms178472.aspx
Assume I have an asp:GridView with some customer information. On that page I also have a button which allows me to add a new user to the GridView. When I add the click the user, I databind the gridview again in the button click. Correct me if I am wrong, but if the asp.net lifecycle or at least part of it is in the following order:
page_load
Hookup event handlers such as my button click
page_preRender
Does this mean that if I put the databinding for the gridview in preRender, that is the only place I need to worry about calling it. I can remove it from the button click. This is what I think, but I am not sure if my thinking is correct, so I would like some more insight as to the benefit of putting code in PreRender as opposed to PageLoad
By PreRender, you assume that most logic in the page that would affect the binding result has been completed (usually in Page_Load, but anywhere earlier really).
Controls in the .Net framework by default do their binding in the PreRender event (makes sense, bind to the datasource at the last possible moment...could be that you changed the data 400 times earlier in the lifecycle). Here's the full layout of the 2.0 lifecycle for reference.
PreRender - Before this event occurs:
The Page object calls EnsureChildControls for each control and for the page.
Each data bound control whose DataSourceID property is set calls its DataBind method. For more information, see Data Binding Events for Data-Bound Controls later in this topic.
The PreRender event occurs for each control on the page. Use the event to make final changes to the contents of the page or its controls.