How to get Label inner text from Codebehind - c#

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.

Related

Getting hidden control of parent from iFramed code-behind

In my parent page I have a hidden control:
<input type="hidden" id="CaseID" value="" runat="server" />
I need for my page in the iFrame to be able to get this value from C# code-behind. I have so far been unsuccessful.
In the child page's code-behind I have tried variations of this:
var theParent = this.Page.Parent;
But I always get null back.
Any assistance would be greatly appreciated.
From the server's perspective, there is no relationship at all between a page which launches an iframe, and the page which is contained within that iframe. They are two completely distinct and unrelated HTTP Requests. In your code-behind, they have nothing in common and there is no way to refer to one from the other.
Thus, you'll need to use the same approach as you would if you needed to "move" data from one page to another. Two common ways (though by no means are these the only ways or even the best ways) are:
The Session object. PageABC can store a piece of data in the session, and PageXYZ can read that data from the session.
URL Parameter on the request. PageABC can call a URL (maybe even use it as the SRC of an iFrame, hint-hint) something like this: PageXYZ.aspx?someKey=someValue. PageXYZ can access URL params from the Request object (Request["someKey"])
Something else to consider: if PageABC and PageXYZ operate in conjunction with each other, maybe having them be separate pages isn't the best approach. It may make more sense for PageXYZ to actually be ControlXYZ and be contained on PageABC. It can still be presented to the user as a popup using jQuery dialogs (or using UpdatePanels and ModalPopupExtenders, if you're masochistic ;).

HTML and jQuery Information Passing Design

I am designing a webapp using ASP.NET and jQuery and I could use some advice.
Currently, the ASP.NET page renders an unknown number of elements that perform an action when clicked. Javascript on the front-end handles the click event based on which specific element was selected.
Each element is embedded with information that the javascript function requires. This informaion is added as extra attributes. So for example, a given element might look like
Link 123
jQuery then attaches a click event and uses the extrainformation attribute as a parameter for an internal function. Each element has 3-5 parameters.
This works great, but I have heard recently that it might not be best practice since it is not WC3 compliant. One possible solution would be for each element to directly call the internal javascript function with the necessary parameters. However this makes me uncomfortable because I lose the separation of concerns between rendering the page and executing the client-side logic.
Is there a better way to do this? Or maybe I'm just overthinking it?
Yes there is a better way, its called HTML5 Data Attributes you can access them from jQuery using the $.data() interface.
For Example:
//instead of
Link 123
//use
Link 123
//then access it like
var info = $('#123').data('info');
alert(info); //alerts: 'something important'
Basically anything starting with data- is stored as data about that element in the DOM, jQuery can access this data via the $.data() function.

How can I pass textbox value from usercontrol(ascx) to another page (aspx) using Server.Transfer()

I am writing one user control (webpart) in kentico. I want to pass textboxes' value from usercontrol to aspx page using Server.Transfer().
Can it be? If so, how can I do like that?
Best Regards,
Reds
I don't particularly like this method, I prefer to use Sessions to pass data between pages, but if you need to do this here how it's done according to the this page.
Here's TL;DR summary. It requires three scripts/pages:
Form.ascx - this will be the control that contains the text box value.
FormParsingScript.aspx (referenced in the Form.ascx in the Action attribute) - this will perform the actual Server.Transfer "FinalScript.aspx" call
FinalScript.aspx which will display the contents of Response.Form["TextBoxName"] (HTTP POST) or Response.QueryString["TextBoxName"] (HTTP GET)

Setting meta:resourcekey on page load

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?

Server-side Javascript (aspx.cs) Attributes.Add Code to Change a Label's text

I am trying to change a label's text by using server-side JavaScript (onclick) and C# within the page_load event. For example, I would like to write something like the following:
Label1.Attributes.Add("onclick", "Label2.text='new caption'")
Does anyone know the correct code for this? Also, what is this type of code referred to; is it just JavaScript or JavaScript in C# or is there a specific name? Lastly, does a book or online resource exist that lists the choices of control.attributes.add("event", "syntax") code to use with C#?
There is no server-side Javascript (unless you change to a platform other than ASP.NET where you actually use Javascript as server language). What you are doing is adding an attribute to the html tag, and the code will be executed entirely on the client side.
First, let's look at how it's done in HTML without the server side code and server side controls:
<span onclick="document.getElementById('Label2').innerHTML='Thank you';">Click me</span>
<span id="Label2"></span>
To use Label controls instead, setting the onclick attribute from server side code, you would do like this:
Label1.Attributes.Add("onclick", "document.getElementById('Label2').innerHTML='Thank you';");
This will work as long as the controls are not inside a naming container. If they are, the id of the controls are prepended with the name of the container to keep them unique, so you need to use the ClientID property to find out what their final id is:
Label1.Attributes.Add("onclick", "document.getElementById('" + Label2.ClientID + "').innerHTML='Thank you';");
The ClientID always contains the id that you can use to access the element from Javascript, so the last code always works regardless if the control is in a naming container or not.
To find out what attributes you can use, you should look at the HTML documentation, for example the Internet Explorer documentation for the span element. When looking at the documetation for a specific feature, notice the Standards Information, as that will tell you if it works in any browser or just in Internet Explorer.
The code above adds JavaScript to a server control rendered on the client. Take a look at this MSDN article - Using JavaScript Along with ASP.NET for more information.
IIRC, you will need to reference Label2 by its ClientID and will need to write some JavaScript to change the label's text value (I think ASP.NET labels get rendered as <span> tags).

Categories

Resources