loading value to asp.net control using javascript - asp.net

iam into problem of reading the value of the control which i alterd using javascript
the sequence goes like this
i got the text box control by using its id
cleared the value of the text box
make the control disabled.
when i tried to retrive the value of the textbox in aspx.cs
iam still getting the old value of the text box which i actualy cleared in the javascript
kindly suggest me to over come this issue
Thanks

Disabled input controls don't get their values posted back so ASP.Net doesn't know you changed the value. You need to enable the control.

With ASP.NET Web Forms, IDs for elements get assigned automatically by the Page on the server side. It is tricky, but possible, to manipulate element values using javascript. One way to avoid this would be to use ASP.NET MVC where the HTML would be rendered as is and javascript or jquery can be easily used to "play" with the HTML elements. We need more information to help you with the details of your request.

Related

ASP.Net 4.0 with TinyMCE and XML Encoding re-encodes Content on Postback

i have a ASP.NET 4.0 based CMS, where i use the TinyMCE (3.4) via jQuery to edit one Textbox.
In addition to this i have several other textboxes.
There is also another DropDown List on the Page, which controls the Contenttype.
This Control has AutoPostback enabled and sets the visibility on the textboxes regarding the selectes item.
As i want to keep the Postback Validation on i have configured the TinyXML to use xml for the content serialisation (encoding: "xml").
Now i have the problem, when a postback from e.g. the DropDown List occures, the re-encodes the content.
Init: "Hallo"
1st Postback: "<p>Hallo</p>"
2nd Postback: "<p><p>Hallo</p></p>"
i have enabled the original textarea via css and this seems to be a problem of the TinyMCS's Save method.
Does anybody have a solution, how to fix this issue maybe with a custom save_callback on the TinyMCE?
Could it be that the data is being reloaded into the tinymce window after it is saved?
When I encountered this before in TinyMCE/WebForms, it was easily fixed by decoding the data before populating the form field and after postbacks:
TextAreaID.Text = Server.HtmlDecode("<p>hello</p>");
I have just had a similar problem with Tinymce and Asp.NET MVC. In my case what was happening was:
The form is submitted and tinymce html encodes the content (I am using the encoding: 'xml' option)
In my server side post action, I html decode the tags I want to allow (simplified example: decodedHtml = model.HtmlContent.Replace("<p>", "<p>")). Then after saving to the database and etc, I update model.HtmlContent with the decoded html (model.HtmlContent = decodedHtml)
but at this point the tinymce editor was showing the encoded html, i.e. <p>test</p> instead of <p>test</p> , even though I did model.HtmlContent = decodedHtml in my post action. What actually happens is asp.net ignores the value in the model on postback and instead binds the posted value (see here http://weblog.west-wind.com/posts/2012/Apr/20/ASPNET-MVC-Postbacks-and-HtmlHelper-Controls-ignoring-Model-Changes for more details on how this works).
A way around this is in your post action to do
ModelState.Remove("HtmlContent");
and then it will bind the value in your view model instead of the posted value.
So in my case, the issue wasn't actually with tinymce but with the way form posts work in asp.net mvc. Hope this helps someone.
Does a look at the entity_encoding setting help?

ASP.NET and Javascript DOM manipulation

I have an aspx page with a set of controls.
A small JS script written on top of jQuery allows the user to drag "li" elements from one list to another.
What I would like now is for my C# code to be able to figure out which items the user has placed in which list after the page is posted back to the server.
Does anyone know how I can do this?
Thanks.
Without some specifics, its hard to get into details. But for ASP.NET server-side controls, the trick is properly maintaining viewstate (or just bypassing it). So strategically you've got two fundamental options:
1) Track the changes server-side by using AJAX to push updates to the server.
2) Track the data client-side by updating some element ASP.NET could understand. My personal favorite is to use a hidden form field that I add a user's "moves" to in a format that can be replayed in my web form.
The easiest way to do it would be to add a HiddenField to your page. Whenever the lists change, populate the HiddenField's value accordingly. When a postback occurs, the HiddenField's value will be available on the server side.
What i've done in the past with drag & drop functionality in ASP.NET, is read the DOM elements via jQuery, and do an AJAX postback with the values in those DOM elements as a parameter.

asp.net 2.0: update formview with jquery

I'm struggling with an issue I just can find a solution for.
First of all, I can't use asp.net AJAX or anything else thant standard asp.net 2.0 as the server admin won't install anything else.
So here is, what I try to do. (For the curious, skip to the bold question below)
My page consists of several parts, each of which gets loaded by jquery.load(url). One of these page parts gets filled with an aspx that contains a form view. As I don't want to have postbacks, I switch to the EditTemplate of the form view by a simple click on a regular html button that submits a parameter indicating the aspx page to switch to edit mode, e.g.
Page_Load(...)
{
if(Request.Params["SwitchEditMode"]) SwitchEditMode();
}
This works perfectly! Now here the part where I'm stuck. The elements in the EditTemplate are based on a select from a database view and bound to the fields by <%# Bind("xx"). Then I have a html button (no asp control) that submits a parameter to the aspx page that tells it to invoke the DataSource update method. In the dataSource_updating method I look for the controls that contain the values I want to save. But these values are always the same, as when I switch to the edit view. No changes I make in the textboxes or dropdowns are preserved.
A long story short, the question how to save the values from EditTemplate back to the database with jquery?
Up to now I tried several approches, that didn't work out.
In the updating() method look for the controls by FindControl and set e.Command.Parameter["xyz"] = foundcontrol.SelectedValue;. The values are always the same as in the beginning.
Set <asp:parameter name="SampleValue" /> and in the EditTemplate <asp:TextBox Value='<%# Bind("SampleValue")#> The values are always null.
Set a hidden input field with the selected value via javascript. This doesn't work as the control within EditTemplate are only visible after the switch into edit mode
So maybe I'm totally wrong with my ideas, heading into a totally wrong direction and this can be accomplished much easier, but up to now I don't know how to achieve this. I could do it without ajax, but for the user experience I'd prefer the version with jquery.
For all that have read this far and not got confused :-), thanks for your effort!
Best regards,
Andreas
I would forget using a form view and just use a regular html form with regular input controls. Return an object from your web service that has all of your values and populate the controls with ajax and then subjmit with ajax. Either do fully asp.net or fully html/jquery with asmx back end. Otherwise it's just too confusing.
If you load both "modes" of the FormView into the same page using AJAX, you're probably getting duplicate field names. One of them contains the unchanged values which are being saved. How will ASP.NET know the difference? You only want to submit the ones from the EditTemplate, which will require a separate form (or some other hack).
Or perhaps your HTML submit button isn't giving ASP.NET what it needs to repopulate the controls. Are you using ViewState in the page with the FormView?
All in all, this sounds like a hairy combination of technologies... as you well know.

I've built a ascx control and I would like to be able to keep adding them using Javascript instead of having to do a full call back

I've built a ascx control and I would like to be able to keep adding new instances of it using JavaScript instead of having to do a AJAX callback. Is this possible? I am basically building a web form for a query control and should clause X be filled in, I want to generate a control for the next clause below. I would like to learn how to do this without doing a callback.
Thanks
ASCX are server side user controls and, to my knowledge, can only be loaded by a server event. This can be accomplished through a full page postback or using UpdatePanels and ASP.net AJAX.
If you don't want to use these options and stick with a full JavaScript solution, you're looking at probably doing DOM manipulation and dynamically adding straight HTML.
If the ASCX controls don't change their appearance and all you're doing is showing and hiding them, one last alternative could be to load all of them into DIV tags that have their display style set to none. Then when the user clicks on a checkbox or whatever, you can use JavaScript to show that DIV tag containing the next control. This is how many JavaScript tab setups work.

Modifying <asp:label> in form

I am developing a form using all asp:textbox and asp:label. Currently, I am using the RequiredFieldValidator to validate text boxes and display an error inline. What I am trying to do is to change the color of one of the asp:labels after validation fails for one of the textboxes fields. Would I accomplish this with javascript, or is there any commands that can do it within asp to modify labels. I would really appreciate any help and code examples.
I'd say that javascript is the way forward here if you don't want to postback to the server. If you use ASP.NET only the page will normally have to be posted back so that the server can change the properties of the label - not very user friendly. A colleague of mine has accomplished this very nicely using JQuery (a javascript libraray) but unfortunatly I don't have a copy of the code to hand. He's an SO user too so I'll pass on a link to this post for you.

Resources