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)
Related
I have a generic html element like this
<span v-bind:class="{ available: days.timeOne }" data-time="10:00" data-date="{{ days.date }}" class="home__visit-featured-days-item-buttons-time">10:00</span>
Which when it is being rendered, is having the vuejs tags being stripped.
I have encountered this issue before when using basic html elements and even control tags like and my solution was to add them manually in the code behind. I don't like this method as not only is long and tedious, it ties back end logic to the view.
Is there a attribute similar to ClientIDMode that I can use to stop these tags being stripped?
ASP.NET webforms will strip out attributes for server controls (those with runat="server") when attributes contain colon (:) characters because these attributes cannot translate to class properties in the back end. However, non-server controls (i.e. raw markup) should just render as written into the ascx file.
Your example doesn't have a runat="server" attribute so I would expect it to render as written. If, however, it is a server control, could you just use raw markup instead?
If it must be a server control I think your only option is to add your attribute in the code behind as you mention e.g. myControl.Attributes.Add("v-bind:class", "{ available: days.timeOne }");
I suppose you are using the CK Editor for entering the HTML code. I wouldn't recommend that since it's WYSIWYG and not a code editor and does such things as stripping some part of the source. If you can, please move your code to Static text web part or to the layout directly. If you need to have it inside the editable region area, you can specify protected source for the CK Editor to let it know what code not to touch:
https://www.google.com/search?q=ckeditor%20protectedsource&rct=j
I recently started working with some legacy ASP.NET stuff, and I've run into an interesting problem:
I've got a table displaying a few values, some of which are evaluated in C# server-side. They all work correctly. Then all of a sudden...
<td><asp:Label ID="Label2" runat="server" class='<%=SevenDayThresholdTooHighOrLow%>'><%=ChgFromSevenDaysAgoInfo%></asp:Label></td>
ChgFromSevenDaysAgoInfo is evaluated properly.
SevenDayThresholdTooHighOrLow is rendered as a string inside of the class quotations. That is
class="<%=SevenDayThresholdTooHighOrLow%>".
In the code-behind file, the two variables are declared in the same scope, and assigned values pretty much one after the other. My Visual Studio doesn't complain about not finding certain variables in code like it would if the property did not exist.
What other factors could be influencing this oddity? What could I have missed?
Thank you very much for your help, everyone!
Eli
EDIT: I took a look at what is the use of Eval() in asp.net and tried to set my tag up that way (class='<%# Eval("SevenDayThresholdTooHighOrLow")%>'), unfortunately to no effect.
That class attribute probably isn't being evaluated for server-side code, likely because class isn't a property of Label. (Label isn't an HTML element, it's a server-side control. So instead of HTML attributes it uses object properties.)
There's a CssClass property, you might try that instead. But even then the approach is different because the system may still not attempt to evaluate server-side code injected into already server-side components. Still, worth a try.
What should definitely work is setting the CssClass property in the code-behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;
If you want to keep this out of code-behind (and who could blame you?) then another approach could be to replace the server-side control with an HTML element entirely. The idea being that if properties on this control aren't otherwise being set in server-side code, then does it really need to be a server-side control? If I remember correctly, a Label emits as a span:
<span class="<%=SevenDayThresholdTooHighOrLow%>"><%=ChgFromSevenDaysAgoInfo%></span>
You can't do that in just the markup code. You can't use server tags in a server control. You could use a data binding tag, but then you would need to trigger the data binding from the code behind anyway.
Just set the attribute from the code behind:
Label2.CssClass = SevenDayThresholdTooHighOrLow;
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.
OK, up front I need to say that I've been coding C# and .NET off and on since Wednesday a week ago. Just trying to figure out enough to fix some scripts provided by a 3rd party. So I'm flying blind here.
I've got a .aspx file (ie, prototype HTML) that contains some tags that need to be dynamically modified from the Page_Load method in the corresponding .cs file. I'd like to do the modification with minimal tear-up of the files involved.
The tags I need to modify are asp:Label, asp:ImageButton, and div. The first two I think I know how to modify, by referring to the IDs in the .cs and setting the associated attributes. But I can't find an "asp:" equivalent of div, nor any obvious way to modify it without that "asp:" equivalent. I could alternatively use a <style>, but I can't find any way to generate that into the HTML stream (though I see hints here and there). I have the Evjen/Hanselman/Rader ASP.NET book, but though it contains some stuff that seems headed in the right direction, that stuff uses HtmlTextWriter which doesn't seem to be the right interface for use in Page_Load.
So, any clues? I need primarily need to modify the style property inside the div.
You can just create an HtmlGenericControl by using
<div runat="server" id="something"></div>
in your code-behind
something.Attributes["style"] = "style it up";
The ASP.NET control equivalent would be an <asp:panel>, but depending on your version, panel used to (1.1) spit out <table>'s for certain browsers.
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.