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
Related
I have a page that does not have runat="server" set in the <head/> section. I do not have access to modify any of the code in the page.
This page contains a user control which I do have access to. Can I add a <meta/> tag to the head section of the page from the user control? It needs to be server-side so a javascript solution won't work.
One option is to create a Response Filter, and then modify the output before it's sent to the user.
https://web.archive.org/web/20211029043851/https://www.4guysfromrolla.com/articles/120308-1.aspx
You can parse the text in
(this.Page.Controls[0] as LiteralControl).Text
to see where the string <head> starts, and insert whatever text you need in there thus injecting your own code into the page header without it being marked with runat="server".
Please be aware though, this is pretty hacky way of getting your code where it most likely shouldn't be (otherwise the <head> element would have been marked as runat="server" so you can access it normally). This will also break if at a later date the head element is changed to be an ASP.NET control. It might will not work with master pages, you will have to walk up the control tree looking for topmost literal element.
I'm currently working on a project where I need to use a lot of AJAX AutoCompleteExtenders, and they have been working fine- but now I'm tidying up the code and implementing a master page, I'm running into issues. I have this JS:
$find('txtName')._onMethodComplete = function(result, context) {
$find('txtName')._update(context, result, false);
webservice_callback(result,context);
};
And when I load the page, this error occurs relating to that snippet:
Microsoft JScript runtime error: 'null' is null or not an object
Just to reiterate, this only happens when I have a master page for some bizarre reason. Any ideas?
You need to use <%=txtName.ClientID%> because with master pages in use your ids will be mangled to avoid namecollision between masterpage and aspx/usercontrols etc.
However please note that to be able to use <%= you will have to include the JS in the ASP.NET markup code. This can be done with the IIS #include rather than using script's src.
Example:
<!-- #Include virtual=".\JS\YourJSFileWithASPNETMarkup.js" -->
Try View Source on your page and make sure that the <input> is still named txtName. Sometimes with master pages, the name changes.
Can you try this:
('<%=txtName.ClientID>')._onMethodComplete = function(result, context) {
If you're using .NET 4 then you could add ClientIDMode="Static" into the page directive, which would tell ASP.NET to keep it's mucky hands off the element IDs.
When you use masterpage, the ID of your controls change, so you can't use the same ID in Javascript, instead you can modify your selector like this : $('input[id*="txtName"]') now it finds all the controls which have an id that contains txtName. there are different selectors of this kind that you can use.
here is a useful link
I am writing a simple personal app that has a browser control and I want it to automatically "Refresh" gmail to check it more often than it does by default. There are monkey scripts that do this but I'm trying to add my personal style to it.
Anyhow, I've looked around and found everything but what I can do in csharp using the browser control.
I found this:
// Link the ID from the web form to the Button var
theButton = webBrowser_Gmail.Document.GetElementById("Refresh");
// Now do the actual click.
theButton.InvokeMember("click");
But it comes back with null in 'theButton' so it doesn't invoke anything.
Anyone have any suggestions?
It's been awhile since I've used JavaScript, but given the other answers and comments that there is no real ID associated with the element, could you do something like the following:
Search all Div's with an attribute of Role == 'Button' and an InnerHtml == 'Refresh'.
Once the correct InnerHtml is found, get the Element.
Invoke the click on the found Element.
Again, this may be blowing smoke, but thought I'd throw it out there.
edit: Just realized you are doing this with C# and a browser control; however, the concept would still be the same.
The best suggestion I could give you at this point involves an existing API that is used for .NET web browser based automation:
http://watin.org/
Since the div tag with the desired button really only seems to identify itself with the class name, you could use the Find.BySelector(“”) code included with the most recent version of watin.
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 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).