Append ASP.Net element via jQuery .append() - c#

Is it possible to do something like this
$("#popupForgot").append("<asp:TextBox ID='emailTextF' runat='server' Width='200px' Text='" + $.cookie("forLogin") + "'></asp:TextBox>")
I need to append an asp.net element to a div tag which will have the cookie value as it's text. I've tried with the code above and it doesn't work.
Thank you in advance

It is not possible to do this, because asp:TextBox is rendered via server-side, whereas jQuery works on client-side.
However you can render your textbox with asp:TextBox control and then you can change its value with jQuery:
$("#emailTextF").text($.cookie("forLogin"));
UPDATE
It turns out that asp:TextBox rendered id isn't equal to id parameter (used inside asp:TextBox), try this:
$("#<%= emailTextF.ClientID %>").text($.cookie("forLogin"));

JQuery is, in the end, just JavaScript code. Javascript code runs in the client's browser.
ASP tags are a server side construct; they will be converted into <input type="Text"/> fields (or something else for multi-line textboxes) before they are sent to the client's browser.
These two are completely separate concepts that can't really interact with each other (directly).
If you want to add a textbox via JQuery it can't be an ASP textbox, it needs to be an HTML textbox. If you really do need to add an ASP textbox (you don't appear to need it from what you've shown here though) then you'll need to do a postback to the server to add it. (You could do that postback asynchronously via AJAX if you wanted.)

Your code will run in the browser, this means that you asp:TextBox will not be processed by the server. So, in short, no.

You can't do this, cause ASP.NET tags processed on the server-side, before data sends to the client.
The ways is:
-add this ASP.NET control before, and change it's value by JQuery,
-if you need to add controls dynamically, use sample html tag , and get data on server from the Form.

Related

Why it's necessary the label asp using ASP.NET?

When I program in PHP, I don't modify the template, but when I use ASP.NET (C#), I must put the asp label for the fields I will use in the server. For exampe: <asp:Button ...>.
Why Microsoft implements this dirty solution?
Theorically, the server will receive all POSTs/GETs from the client, with the asp label or not.
It is not necessary the usage of the asp.net controls. Actually, you have the opportunity to use html controls, when you don't need the asp.net controls. It depends solely, on what you control would hold and how you will use it. For instance, if you want to declare a label, with a constant text value, you could use an html label
<label>LabelName</label>
On the other hand if you want to declare a label, whose value will be change, when one or more events will be fired, then you have to use the asp.net controls
<asp:Label ID="labelID" runat="server"/>
Then you can access in the server side the value of this label as labelID.Text.
Last but not least, you can update the value of an html label using javascript in the client side, when again one event or more events will be fired. In order to achiene this, you have to declare it like below:
<label id="labelId">LabelName</label>
Then using javascript
var label = document.getElementById("labelId");
you could select this label and you can access it's value as
label.innerHTML
or more easily using JQuery,
var label = $("#labelId");
var value = label.val();
So it depends on what you want to do. That will lead you to select the proper control. You don't have in any way to select all the time asp.net controls or hmlt controls.
Well using word like dirty is too much to say when you doesn't understand that what you are doing in ASP.NET. in PHP, you write HTML and manipulate output HTML from php code. In ASP.NET you write a code that write HTML afterwards. Basically your should read more about difference in two platform. To explain it a bit ASP.NET can be written using PHP.
ASP.NET provide asp tags, not just for label but for Grids as well, when you use them one liner tag you often get complex HTML as output, and these output HTML is modified based on browser settings. Also, ASP.NET have Standard HTML tags you are free to use them at will. Having ASP tag with runat=server you actually use them in Code Behind, if not, just do the dirty PHP trick to put inline PHP in HTML.
So, read about it, and you find it make sense to have that dirty solution.
You don't have to. you use asp prefix for asp.net specific controls. Check this:
<asp:Label Text="Test text" runat="server"/>
<span runat="server">Text=Test text</span>
Both will generate same markup, but Label has more properties which might be used(Localization one of the benifits)

how to set attributes by jquery and access them by c#

I wanna change or add custom attributes in client side by jquery and check them in server side by c#
(for example add a custom attributed named "Loaded=1" to an image HTML element when the image is loaded successfully and in server side I access to this attribute through Image control Attributes Collection)
Is it possible ??
Thanks for your feedbacks
I think you have a few Page Lifecycle aspects mixed up, the attributes that you set on client side never end up on server side, unless you post them to the server. I think you should consider adding hidden fields to forms via jQuery so that when the form is posted the values arrive at the server. Then server side you'd need to interpret the posted values and turn them into attributes on the correct Image objects.
Consider something like this:
Create the following element with jQuery:
<input type="hidden" id="Image23_Loaded" name="Image23_Loaded" value="1" />
Post this to server and parse it so you know which Image object should get the Loaded attribute.
Find the Image object and add the attribute server side.
You can also use asp HiddenField control. set it's value on client side and you can access it on server side. If your HiddenField id is "hdnImage1" you can set it's value on jquery like $('#<%= hdnImage1.ClientID %>').val('1');.
Hope this help.
You may also post a JSON result object to the server using AJAX and de-serialize it to an .net object.
take a look at: https://stackoverflow.com/a/2246724/952310
you can't do that. Once the page has been transferred, the server side cannot access the page
You can however, transfer data using $.post(url,data, callback)
data can be obtained from a form or serialized array

Yet another ASP:Textbox vs <input type="text">

I have read through some articles on this topic but I am still cautious about this. I am all along using ASP:Textbox but I would like to know what are the things an input textbox cannot possibly perform without using a ASP:Textbox or takes much more effort to pull off?
I have a Jquery tooltip sample which uses HTML input textbox and I am not sure if I should change all my ASP:Textboxes to HTML textboxes, the things which I need to perform on this textboxes are RequiredFieldValidation as well as storing their values into the database.
Anyone can advice me on this rookie question. Thanks.
The asp.net textbox IS a html input box from jQuerys point of view. Everything special about it is done on the server (including viewstate validation). The question you need to ask, is what value does giving it a server side reference bring to your app? Generally the answer is easy server side reference, but does that apply in your case?
As far as i know there is no visible server side programming difference between a HTML input box and an ASP Textbox. ASP.NET Validators would work perfectly both ways.
As for your tooltip concern. Jquery doesn't care if your using an ASP Textbox since your ASP textbox will end up as an HTML textbox anyways.
And for the advice - i don't recommend you changing all your ASP Textbox to HTML input boxes, its just a waste of time. You should use the CSSClass / class property to display the tooltip instead. That way it would work on your regular HTML input box and ASP Textboxes + other page elements.

Display Data Based on Drop Down List

How would I go about using c# and ASP.NET (or Javascript if need be) to have my drop down lists change something like a div on the page. Basically I want it to populate a div with certain data.Either from a SQL or XML Data Source. I have places I wanted listed, so I'm assuming I'm going to need to put each place in with the tags that it may contain.
I would use jQuery, or some other javascript library, to hook up a listener to your dropdown and when the value changes I would make a request to some services or where you get your data from.
If you're using asp.net, then it's really straight-forward.
1) put a <asp:label> or <asp:literal> in your div.
2) in your designer, double-click your dropdownlist -- this will take you to the code-behind for the dropdown's onselecteditemchanged event.
3) Call your data, and assign it to the label. Ex.: ...me.myLabel.text = myreader("myfield"), or some more complicated html you've assembled, or what have you.

Get HTML using JQuery from asp.net button click?

I have a page with a table on it with some information. I also have an asp.net button that I use to send an e-mail with the table markup. Currently, I am regenerating the table and storing it in a string and sending that as the body of the E-mail. What I would like to do instead is use JQuery's html function to grab the table markup of the page and store that in the variable to be used the e-mail body. Can someone provide an example?
There are a good amount of ways you could construct something like this. A quick search of JQuery syntax returns the method with which you can return an elements HTML contents:
$('div.demo-container').html();
http://api.jquery.com/html/
It would be possible to construct your Javascript in a way which grabs the return call from the html() method in JQuery and stores that value in a asp:Hidden before posting your form back to the server. Your postback reference to that hidden field would then include the markup you desire.

Categories