I have this problem: From a database, held up a string, which contains HTML mixed with C# code. I wish I could run correctly both codes on my page .aspx.
e.g.
in my .aspx:
<div><%= Model.repo.getCode() %></div>
and the getCode() method give me this:
<div id="secondDiv"><p><%= Model.Person.Name %></p></div>
so I want the final html file look like:
<div><div id="secondDiv"><p>Jhon</p></div></div>
any suggestion?
There may be direct way to bind such value,
But if you could store String.Formatable into database then it would be easy to bind the data needed.
Using String.Format you achieve like,
returned string from Model.repo.getCode() (see curly braces)
"<div id="secondDiv"><p>{0}</p></div>";
And in ASP code,
<div><%= string.format(Model.repo.getCode(),Model.Person.Name) %></div>
Take a look at this project as it helped me with a similar problem: https://github.com/formosatek/dotliquid Basically you can bind whatever objects to a template and that template can call properties of you objects and even use conditional logic and loops.
Related
I have html label contol without runat="server"
Does it possible to get inner text from code behind c#?
Label:
<label id="lblClanName">Text Here</label>
Thanks
Every time an ASP.Net page is posted back to the server it is recreated from scratch using the custom code contained in the page (such as calls to a database), the HTTP post/get collections (which include ViewState), any custom data in Application, Cache, Session, static objects, etc.
If the value does not exist in any of those locations, the server doesn't have access to it. A common trick to pass data from the client is to simply use a hidden field. If you want something more elegant, you can use asynchronous AJAX to send/receive data from the server.
Or in this case, you could just add runat="server" to an asp:Label. ViewState will maintain the value between postbacks, though it will not reflect changes made client-side unless (once again) the data is somehow passed back to the server.
Note that ViewState is typically a bad thing because it essentially doubles the size of your data (or more) and (in my opinion) encourages sloppy design.
i don't think you can do it.either you can use js get the lable,and call js method from code behind
Short answer: no.
To access this from your code-behind, you will minimally need to add runat="server" to your label. This will allow you to access it using Page.FindControl(String).
The preferred approach, if you are able to modify the front-end code, would be to use an <asp:Label />. This will allow you easy access by just using the control's ID in the code-behind, specifically its Text property.
Do you want to know how to parse a string value for the inner html, or do you expect your web page do have text written to the label at runtime?
string labelHtml = "<label id="lblClanName">Text Here</label>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(labelHtml);
string innerText = doc.DocumentElement.InnerText;
Why do you need the text between a label, is this for a live web page? This sound like a bad design more than a requirement.
I am trying check if the inner html of the element is empty but I wanted to do the validation on the server side, I'm treating the html as a string. Here is my code
public string HasContent(string htmlString){
// this is the expected value of the htmlString
// <span class="spanArea">
// <STYLE>.ExternalClass234B6D3CB6ED46EEB13945B1427AA47{;}</STYLE>
// </span>
// From this jquery code-------------->
// if($('.spanArea').text().length>0){
//
// }
// <------------------
// I wanted to convert the jquery statement above into c# code.
/// c# code goes here
return htmlSTring;
}
using this line
$('.spanArea').text() // what is the equivalent of this line in c#
I will know if the .spanArea does really have something to display in the ui or not. I wanted to do the checking on the server side. No need to worry about how to I managed to access the DOM I have already taken cared of it. Consider the htmlString as the Html string.
My question is if there is any equivalent for this jquery line in C#?
Thanks in advance! :)
If you really need to get that data from the HTML in the ServerSide then I would recommend you to use a Html-Parser for that job.
If you check other SO posts you will find that Html Agility Pack was recommended many times.
Tag the SpanArea with runat="server" and you can then access it in the code behind:
<span id="mySpan" class="spanArea" runat="server" />
You can then:
string spanContent = mySpan.InnerText;
Your code-behind for the page that includes this AJAX call will have already have executed (in presenting the page to the browser) before the AJAX call is ever executed so your question doesn't appear correct.
The code-behind that is delivering the HTML fragment you indicated is probably constructing that using a StringBuilder or similar so you should be able to verify in that code whether there is any data.
The fragment you provided only includes a DIV, a SPAN and a STYLE tag. This is all likely to collapse to a zero width element and display nothing.
Have a look at this article which will help you understand the ASP.NET page life cycle.
http://booking.travel24.com/index.php?KID=610000&&id=lmpergebnis&showresult=1&detail=zielgebiet®ion=-1&ziel=-1&termin=20.02.2011&ruecktermin=17.03.2011&dauer=-1&abflughafen=46&personen=25;25&kategorie=-1&verpflegung=-1&zimmer=-1
I am trying to parse some HTML parts of this page, but when I check the source code I can not find this: "Tunesien, Marokko".
If I check with xdeveloper I can see this as html:
<a class="reglreg" href="javascript:s_hliste(20009);">Tunesien, Marokko</a>
but if i check source code of the page I can't find this. Why?
If you view the source and search for "Marokko" you will see there are several places where it occurs (loaded as data in several JavaScript arrays).
It appears as if some of the content is produced dynamically through the JavaScript loaded onto the page. That JavaScript builds HTML and changes the page to include the content you are looking for.
To answer your first real question
Why?
Because when you check the source code inside a browser, you'll get the original html code. Then javascript comes along and modify the DOM which you can follow in any modern browser's console.
can i get somehow whole source code
then? if i can not see it in browser
how can i see it?
To make it simple, it depends how you're trying to parse it. With what language?
maybe the data is coming via AJAX call, so it's not there on the html at the start, but dynamically added to it.
if you need to parse this, you can try to "emulate" the ajax call yourself.
I have a label on a page which gets localized text through the meta:resourcekey attribute. The issue I have is that I want it to display different text depending on which view of a multiview they're on.
I tried adding the attribute though label.Attributes.Add("meta:resourcekey", "label"), but that doesn't seem to load any text. I tried it on PreRender, and same deal. The attribute appears when I look at the source, but no text is displayed.
Is this possible to do? The other option is to have 2 labels and change the visibility on page load, but that seems like the less elegant solution.
Thanks.
I think what you want for programmatic localisation in code behind is as simple as this:
ctrl.Text = (string)GetLocalResourceObject(“myCtrlKey.Text”);
ctrl.AnotherAttribute = (string)GetLocalResourceObject(“myCtrlKey.AnotherAttribute”);
Using LocalResource means that for a page called MyPage.aspx, you have created a resource file called MyPage.aspx.resx and/or MyPage.aspx.{culturename}.resx in the special directory App_LocalResource.
If you like Global Resources instead of local, use the special directory App_GlobalResource
to hold a resource file called MyResourceFileName.resx and call:
ctrl.Text= (string)GetGlobalResourceObject(“MyResourceFileName”, “myGlobalKey”);
copied from a blog about localization in the code behind
--
PS the reason that Attributes.Add("meta:resourcekey", "label") doesn't work is that "meta:resourcekey" isn't a real attribute and its use in the aspx is not really valid aspx markup - rather it's a preprocessing directive that causes the compiler to turn it into a longer list of attributes name/value pairs, based on what you've put in your resource file.
The approach of trying to assign a meta:resourcekey attribute will not work simply because they are treated specially by the page parser, and replaced before the page lifecycle code even really begins.
But meta:resourcekey is basically a declarative replacement for the code equivalent of accessing local resource files. In other words:
<asp:Label ID="MyLabel" meta:resource-key="MyResourceKey" />
is equivalent to:
<asp:Label ID="MyLabel" Text="<%$ Resources: myResXFile, MyResourceKey %>" />
is equivalent to the code:
MyLabel.Text = Resources.MyResXFile.MyResourceKey;
It looks like you're already dealing with your label in the code if you're trying to assign attributes to it. Why not set it's value in the code?
I saw an example of using Expression Builders, and creating your own Custom Expression Builder Classes here:
https://web.archive.org/web/20210513211719/http://aspnet.4guysfromrolla.com/articles/022509-1.aspx
However, I fail to see the value in using this approach. It doesn't seem much easier than programmatically setting values in your code behind.
As far as I can tell, the only thing you can do with them is set properties. Maybe they would be useful for setting defaults on certain controls?
Can anyone shed light on where this ASP.NET feature becomes powerful?
We are using a custom expression builder to localize our application. E.g. the markup looks like this:
<asp:LinkButton Text="<%$ Str:SomeString %>" ... />
The expression builder reads a string with the ID SomeString from a resource file (taking into account the current user's language preferences) and assigns it to the Text property of the LinkButton.
This is quite flexible: we can add languages by simply copying a resource file into the application directory. And if a customer wants to have a different text for that linkbutton, he just adds his custom string to the resource files and changes the string-ID in the expression builder (without having to change the code-behind).
Custom Expressions are handy if you care about ViewState (you should). See TRULY Understanding ViewState.
It is useful when you need the expression to execute early in the page life cycle. It executes when the parameter is needed not at a particular point in the page life cycle.
Also have a look at making a general purpose 'Code' expression builder.
Making some client side javascript parameters "dynamic", is a good use for this feature.
So say you have a setting in a web.config file that you want to make its way down to a client in a javascript tag. You could handle the OnRender event in code behind and muck around with the js there but that would be ugly. Much nicer to do something like this in the ASPX:
<script type="text/javascript">
var sessionKill = <%$ AppSettings:ClientSessionTimeOut%>