Can you set a asp.net web user control property using jQuery? - asp.net

Can you set a asp.net web user control property using jQuery

I don't think it's possible, since the controls' properties are evaluated before the PreRender phase. Every property setting made by jQuery happens after the page is rendered so it's useless.

Related

Asp.net Viewstate not saving a control's style

I have read that the viewstate in asp.net stores the values of control properties across postbacks.
Lets say I have a page that has a textbox
<asp:TextBox ID="fldFileId" runat="server"></asp:TextBox>
and then on the client side via javascript, I get a reference to the element , and then set the border style thusly
refToTextBox.style["border-style"] = "dashed";
Upon postback, the border style has dissapeared and the textbox reverts to its original look. But glancing at the properties for an asp:TextBox in VS2010 there is a 'BorderStyle' property for it.
Is there a reason why this attribute does not get saved in the view state?
Setting a value client-side does not update ViewState. You have to set the style server-side for ViewState to store it. Alternatively, you could:
Re-run your JavaScript after postback.
Store the styling in a cookie and use JavaScript to restore the style.
Find a JavaScript library to modify ViewState on the client-side (not recommended).

why asp.net dynamic charts are rendered only after the second page_load?

I am using web forms to develop a web application.
From the very beginning, I have always been surprised by the page_load event being fired twice, and finally today I found out that all gridviews and texts are rendered after first page_load, and it takes another page_load to render all the dynamic asp.net charts..
why is it so, is there an attribute on the chart web server control that I can use to bypass this?
Check to see if you "AutoEventWireup" set to true. If this is a control, check the parent control/page also. Even if the control itself is set to false, but the parent is set to true, it seems to fire anyway.
Also check where you assign listener to Page_Load event:
this.Load += new System.EventHandler(this.Page_Load);
Should be in InitializeComponent method

Enable or Disable a Control without PostBack asp.net

Greetings, I have an ASP.Net form with some standard controls (DropDownLists and TextBoxes) that I need to be disabled or enabled based on the checked state of a checkbox control.
I currently have it working via the CheckChanged event on a postback.
Can I use Ajax to accomplish the same thing without Posting Back?
You can use Jquery or Javascript to Hide or disable the controls client side.

Detecting client-side DOM changes server-side in ASP.NET: Is It Possible?

I'm working on developing a custom control to select items from a predefined list. This is accomplished via 2 ASP.NET ListBox controls, with a few buttons to trigger the movement of ListItems from one ListBox to the other (lets call these ListBoxes lstSelected and lstDeselected).
This is easy enough to do in ASP.NET or JavaScript independently: I have both working. However, if modifications are made via JavaScript, ASP.NET retains no knowledge of this. Is there any way to register the creation of of options in a select tag without AJAX?
You could also do this with traditional postbacks, it doesn't have to be ajax. The postbacks would be triggered by clicking your buttons which change which items are in which listboxes.
You could have a couple of hidden fields, say hdnHasSelectedChanged and hdnHasDeselectedChanged, and set those fields in your javascript code. Then, when a postback really happens, your code-behind can read those hidden fields to detect if changes occurred.

Add dynamic controls in ASP.NET, is there a difference between 1.1 and 2.0?

I am pretty sure back in the days of ASP.NET 1.0/1.1, controls created during runtime needs to be added before Page_Load event of the Page Lifecycle (i.e. inside Page_Init).
Here's one article by Microsoft on it (for .NET 1.0/1.1):
HOW TO: Dynamically Create Controls in ASP.NET:
Note When you create dynamic controls
on a Web Form, you must create the
controls and add them to the controls
collection in either the Page_Init
event handler or the Page_Load event
handler. Otherwise, the controls may
not behave as expected.
However, in a few posts here, it seems like the above is not the case anymore. Controls added within Page_Load seems to be working for everyone else. Some of the posts include:
creating dynamic control in asp.net
Viewstate - utter confusion.
I've tried it myself and indeed it worked though I've not done enough test to fish out any unexpected behavior.
So is Page_Load a safe stage to add dynamic controls? Or is it only for .NET 2.0 and above?
I have studied this with Reflector, and the Control class does indeed bring things up to speed when you add them dynamically, no matter when you add them. It does everything - loads viewstate/controlstate, calls postback events, calls event handlers, etc. I don't know if it was different in ASP.NET 1.x days, but in 2.0 and above this is the case.
As for the "dangers" - there are some gotchas that the inexperienced user might trip over, so it is recommended that you add them in Page_Init or before. (Note that the PreInit event only applies to the page itself, not the Master Page or subcontrols). Off the top of my head (I'm sure there might be a few more):
By default viewstate loads positionally. That is, it ignores control IDs and just takes control placement in the tree into account when loading viewstate. If your dynamic controls were present when the viewstate was serialized, but are not present when it is deserialized, the wrong viewstate item might get assigned to the wrong control, thus leading to exceptions. This can be changed by some settings, though I'm now too lazy to search for them.
Since the "bringing up to speed" happens when the dynamic control gets added to the page, the order of some events might be unexpected. For example, if you add a TextBox control to the page in the Page_PreRender event, the Changed event of the TextBox will happen there and then. If your event handler code depends on the event happening with the rest of them before PreRender, then you are screwed.
You can add controls at any time. However, they'll only work with viewstate if you add them before page loads.
In fact, if you check the .Net 2.0 version of the page lifecycle link you posted, you'll stilll find this quote under the PreInit event:
Use this event for the following: ... Create or re-create dynamic controls.
The Page_Load event handler is an acceptable place to add controls. If you re-read your note you will notice that they state that.
Note: When you create dynamic controls
on a Web Form, you must create the
controls and add them to the controls
collection in either the Page_Init
event handler or the Page_Load
event handler. Otherwise, the controls
may not behave as expected.
If the ASP.NET 2.0 article you linked to, under "Catch-up Events for Added Controls", they discuss how added controls are brought up to speed with the page.

Resources