Why $find doesn't work? - c#

This is my code:
// user control
<asp:CheckBox ID="chbGenerali" runat="server" />
// js
var prova = $find("chbGenerali");
console.log(prova.checked);
but I get TypeError: prova is null. It should be a sort of:
document.getElementById("<%=chbGenerali.ClientID%>")
isn't it?
Do I need to enable somethings

ID's set compile time do not always equal the ID at runtime by default.
As you are using framework 4.0, you can use the ClientIDMode property on the control.
ClientIDMode="Static"
Read this weblog article for more information about the ClientIDMode property.

Basically, the client side ID is not the same as the server side, because of the control tree handling. ASP.Net generate client side which are the combination of the hierarchy of the control Id's. This will ensure uniqueness of Ids.
For example, if your control is in the panel "pnlABC", and your checkbox is named "chkGenerali", the output clientID will be "pnkABC_chkGenerali". Extend this logic to the whole control tree to get the actual ID.
The simpliest way to solve your issue is to replace your code by:
var prova = $find("<%= chbGenerali.ClientID%>");
which will generate the actual client side ID at render time.

Related

Create User Control is .Net without predictable ClientID Mode

I am creating a fieldset user control which makes it easier to use in my web application.
However, since it is a user control it needs a unique ID to create the control and the child controls inside of it
I have a ASP Placeholder control to allow the user control to accept child controls, but creates a longer and longer ClientID that isn't really necessary as this is standard .NET programming.
Is there a way to turn off this automatic referencing within the my user control and still retain the referencing in the parent object which the fieldset is in? (It is in a masterpage by the way...)
What I am asking is --- is there a way to create a usercontrol without the child elements of the usercontrol having their ID based on the parent usercontrol... I notice that the ASP UpdatePanel has this ability and want to replicate that same functionality.
Much appreciate your feedback.
There are a few ways to deal with this there is a good resource at 4guysfromrolla
However in summary some of your options are :-
The first is that you could make all your client id modes static - have a look at the ClientIDMode in web.config
Don't make predictable client id modes but use the Id of them in your code such as
var txt = document.getElementById('<%=TextBoxID.ClientID%>');
So here you would inject the client Id using ASP.NET directly into your HTML.
Override the client ID to be static for each property - this is where you can specify in .NET what you want the client ID to be foreac control you are using.
<asp:TextBox ID="txtName" runat="server" ClientIDMode="Static" />
Hope this helps!

How can I get the Server Side ID of a UserControl?

I need to do a __doPostBack in a WebForms of this LinkButton:
<asp:LinkButton ID="AggiungiSocial" runat="server" onclick="cmdAggregaSocial_Click">Link</asp:LinkButton>
I see that .NET process it as:
javascript:__doPostBack('ctl00$ContentPlaceHolder1$Dashboard1$RepeaterSocials$ctl00$AggiungiSocial','')
so what I need is javascript:__doPostBack('ctl00$ContentPlaceHolder1$Dashboard1$RepeaterSocials$ctl00$AggiungiSocial','').
But rendered, the ID is instead ContentPlaceHolder1_Dashboard1_RepeaterSocials_AggiungiSocial_0
So Do I need to do a javascript replace/parse of that ID or is there a method to get this "__dopostback" UserControl id?
Such as:
var ServerIDUserControl = link.attr('href').replace("javascript:__doPostBack(", "").replace("','')", "");
(which is terrible imo).
(Perhaps depending on which (ASP).NET version you're using) the name attribute value will contain the string you're looking for, as opposed to the id being made identifier-friendly. An example from a 3.5 site here at work looks like this for a Button:
<input type="submit"
name="ctl00$cph_main$ctl00$PerformSearch"
id="ctl00_cph_main_ctl00_PerformSearch">
I parenthesise the first part simply because I have to admit my ignorance as to when/why this happened - I recall a time when this wasn't the case, and it could be either a 3.5+ thing or a configured thing.
Otherwise you could use the inline server-side script syntax to output the control.UniqueID in the appropriate place.
To get the id of the control sent as __EVENTTARGET during postback you need to use the UniqueID property of Control. In your case:
javascript:__doPostBack('<%= AggiungiSocial.UniqueID %>', '');
However, from your example I see that your control is nested inside a repeater called RepeaterSocials. In this case, your code might look like this:
javascript:__doPostBack('<%= RepeaterSocials.Items[0].FindControl("AggiungiSocial").UniqueID %>', '');
But my advice is to not use this approach. You can instead capture the Repeater.ItemCommand event and see which button was clicked inside the repeater.

How to get Label inner text from Codebehind

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.

ClientIDMode How to hide ID in Html Source code generated in browser

I use .net 4 and c#.
I have a asp.net Page wit an HyperLink.
<asp:HyperLink ID="uxLink" ClientIDMode ="Static" runat="server"></asp:HyperLink>
In my code behind I use the HyperLink ID="uxLink" to change some properties.
In my browser the code generate is:
<a id="uxLink" href="/blog/5/test.aspx">Test link</a>
I would need omit the id from the source code generated in the browser so it should look:
Test link
I tried to play with ClientIdMode but with no succe... Any idea how to do it? Thanks
If you remove ClientIDMode ="Static", the framework will generate a "big unique" ID for the element, but still, there will be an ID.
Having an ID is fundamental if you want to handle the elements in client-side script. The IDs should not be removed.
If you don't want them, just don't use ASP.NET components, use the <a> tag itself.
Update
By the way, supposedly there's a way to do what you want in ASP.NET 4. It's described here: http://www.codeproject.com/Tips/208323/Disable-ClientID-for-any-control-on-ASP-NET-Page?display=Print
I haven't tried it, I'm just pointing you into a direction. You should be aware of the implications of not having a ClientID, though. Specially for Postback controls. Read the article.
The easiest solution is to change the ID propriety for the control in code behind before rendering the page to NULL.
in my example:
myLink.ID = null;
This would generate no ID at the Browser resulting in:
Test link
I find this useful trick on this website:
http://www.dotnetperls.com/remove-id

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