How to control usercontrol from javascript - asp.net

I have an usercontrol with an attribute targetUrl. I add this user control to a page and write targetUrl attribute from this page like below:
<PBG:Modal ID="Modal1"
runat="server"
Height="180"
Width="500"
src="pop_adres_giris.aspx"/>
This worked properly, but I want to change the targetUrl attribute from javascript. And I can't do it. I write code like below, but it didn't work.
var frm = document.getElementById('Modal1');
frm.targetUrl = 'pop_adres_giris.aspx';
How can I do it?

The UserControl object, which generates HTML on the client side, are not accessible as the rich objects which are available when handling server side calls.
Depending what the UserControl is, you will need to use a different method to get it and set the "targetUrl".
In addition, to ease your accessing of elements within the DOM you may want to consider using a library such as jQuery or prototype
Once you have declared your control, for instance, if you were using an asp:Hyperlink control:
<div id="hyperlink_holder">
<asp:Hyperlink ... NavigateUrl="http://someurl" />
</div>
You know that asp:Hyperlink generates html like <a href="http://someurl" ... />
So we can access the element and change the link like:
$('#hyperlink_holder a').attr("href", "http://newurl");
In addition, note that the ID you give an item in ASP.NET is not necessarily the ID which will render in the id element in the HTML; it is instead a concatenation of a number of ids; therefore use selectors based on non runat="server" controls where possible, or pass the ClientID of the UserControl through to the client to use for selection if absolutely necessary.

Related

How to prevent asp.net to make lowercase my custom attribute I placed in a server control

I have an aspx page and controls on it. For the purpose consuming it at client side javascript I would like to place attributes for my elements. I would like to do it all in the markup, not in code behind. It seems the placed attributes will be all lowercased at client side.
For example:
<asp:LinkButton ID="anyButton" runat="server" confirmationMessage='any message'...
will be rendered at client side as:
<a confirmationmessage='any message'...
Consequently I must use this casing all my javascript code. Not a big issue, but quite disturbing if you like strict naming conventions.
Thanks in advance

When isn't runat="server" used in asp.net?

When don't you need to use runat="server" in ASP.NET?
EDIT: Thanks for all the answers, but I was really thinking of runat="server" inside an <asp: tag.
Use the runat=server attribute when you're using ASP.NET controls, and/or you require programmatic access to those controls in your code-behind.
HTML controls don't require this attribute. It's useful if you have any HTML element like <span> <div>, or <table> when you want the ability to access them in code-behind.
<asp:Label runat="server" id="foo" />
<div runat="server" id="bar />
...
foo.Text = "Hello Label";
foo.Attributes["class"] = "baz";
You need to use runat="server" on any control that you want to be parsed as a server control.
Any element with runat="server" will be parsed into a server control in the Page herarchy. Anything else will be handled as plain text, and put in LiteralControl controls in the Page hierarchy.
The exception is elements that aren't real elements, but special tags within another server tag, for example ContentTemplate tags. They don't need a runat="server" because the containing control will parse them.
When you don't want the server side ASP.NET to render a server side variable against us.
Generally speaking you don't use it when you don't need to manipulate the DOM element at the server side e.g. which are only used for layout purposes.
Without runat="server" there would also be no other way to make html controls server side controls. It does look like an odd thing, because you can't do runat="client".
So in summation you can't leave it out on any ASP .Net controls ever and it was probably the easiets and cleanest way to find all server side controls for the developers who created ASP .Net Web forms.
source: http://mikeschinkel.com/blog/whyrunatserverforaspnetpart2/
Tag runat="server" indicates that the code contained within the script block will run on the server (and not on the client). On execution, ASP.NET will create server-side objects that contain this code as well as an instance of the Page class to contain the controls defined inside the page as instances of their given type (System.Web.UI.WebControls.Textbox, for example). This server-side object will be invoked on user request and will execute code in response to events.
Create Control in Runtime
I need one label in runtime that time don't need runat="Server" is not required
Example
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label lblNew = new Label();
lblNew.ID ="lblnew";
lblNew.Text ="Test";
}
}
this code create label in runtime at page load event

hyperlink.NavigateUrl getting changed on master page (possibly by ResolveUrl() )

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:
hyp1.NavigateUrl = "#notificationPopup";
Which renders as:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.
Any ideas?
There is a note on MSDN Control.ResolveClientUrl method description.
The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.
So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?
The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:
Example
In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:
UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.
<html><head><title></title></head>
<script type="text/javascript">
function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}
</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
<a href="" onclick="navHyperlink(this);" >click here</a>
</html>
Code behind would be:
hdnHypNav.value = "#notificationPopup";
You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...
ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
I found another way to solve the problem.
hyp1.Attributes.Add("href", "#notificationPopup");
Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.
My fix was to enclose the hyperlink in a literal:
<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
Example
</asp:Literal>
The downside is if you need to programmatically manipulate the link, it's a bit more annoying:
lit1.Text = String.Format("Example", HttpUtility.HtmlAttributeEncode(url));

How to fill a Label.Text - Property via jQuery

I use ASP.NET and have a label control on my page, which I fill with
the jQuery-Command
$('#<%= myLabel.ClientID %>').html(content);
.val() does not seem to work with this.
Somehow, I have Problems getting the content in code-behind. In the code, the myLabel.Text-Property is still empty.
If you want to display the value on the client and have it available on the page, you need an input that'll get sent to the code-behind when you POST like this:
$('#<%= myLabel.ClientID %>').html(content);
$('#<%= myInput.ClientID %>').val(content);
<asp:Label Id="myLabel" runat="server" />
<asp:HiddenField ID="myInput" runat="server" />
In the code-behind:
myInput.Value
I think your problem is that labels (rendered as span tags) are inherently read-only in the asp.net world. They're not meant to be used as 'input' controls, and as such changes to their HTML on the client-side are ignored on the server-side, where values are set based on ViewState.
To do what you are asking, you'd have to notify the server of the change as well, such as by using AJAX. The only issue here is ajax webmethods in your code behind are static, and because of this can't access the page's control set to change the .Text value.
In the end the easiest option is to make use of hidden fields as Nick said. These are technically 'input' controls and their values changed on the client-side are sent to the server as you desire. You'd just have to keep the label/span and hidden field/input synchronized on the client.
Hope this helps.

How to stop ASP.NET from changing IDs in order to use jQuery

I have this label control in my web page
<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
And when the page rendered the id of the control changes to something like this
<span id="ctl00_ContentPlaceHolder1_Label3">test</span>
How can I stop asp.net from changing IDs in order to perform a jQuery operation like this
$('#label1').html(xml);
Short answer:
Set ClientIDMode="Static"
<asp:Label ID="myLabel" ClientIDMode="Static" runat="server" Text="testing"></asp:Label>
Long answer:
.NET 4 now has the ability to let you choose your ClientIDMode:
Type: System.Web.UI.ClientIDMode
A value that indicates how the ClientID property is generated.
AutoID
The ClientID value is generated by concatenating the ID values
of each parent naming container with the ID value of the control. In
data-binding scenarios where multiple instances of a control are
rendered, an incrementing value is inserted in front of the control's
ID value. Each segment is separated by an underscore character (_).
This algorithm was used in versions of ASP.NET earlier than ASP.NET 4.
Static The ClientID value is set to the value of the ID property. If
the control is a naming container, the control is used as the top of
the hierarchy of naming containers for any controls that it contains.
Predictable This algorithm is used for controls that are in data-bound
controls. The ClientID value is generated by concatenating the
ClientID value of the parent naming container with the ID value of the
control. If the control is a data-bound control that generates
multiple rows, the value of the data field specified in the
ClientIDRowSuffix property is added at the end. For the GridView
control, multiple data fields can be specified. If the
ClientIDRowSuffix property is blank, a sequential number is added at
the end instead of a data-field value. This number begins at zero and
is incremented by 1 for each row. Each segment is separated by an
underscore character (_).
Inherit The control inherits the ClientIDMode setting of its NamingContainer control. This is the default.
instead of using this selector
$('#Label1')
use this one, basically you are using the classic asp inline server side code.
$('#<%= Label1.ClientID %>')
this will insert whatever ID that is generated to be placed as a literal.
If you wish to use external files then I would suggest you create an obj that is global on the page to hold all of your client ids and then reference that obj within your external files (not ideal but it is a solution)
var MyClientIDs = {
Label1 = '<%= Label1.ClientID %>',
Label2 = '<%= Label2.ClientID %>',
Textbox1 = '<%= Textbox1.ClientID %>',
Textbox2 = '<%= Textbox2.ClientID %>'
};
and then in your external file you can access Label1 for example: $('#' + MyClientIDs.Label1)
In .NET 4+ set ClientIDMode="Static". This will solve your problem.
Example:
<asp:Label ID="Label1" ClientIDMode="Static" runat="server" Text="test"></asp:Label>
You can't stop asp.net from generating those IDs. That's just how it does things.
You can still use jquery like so:
$('#<%=label1.ClientID %>').html(xml)
or
$('[id$=_label1]').html(xml)
If and only if you're container layout is never going to change and you require to put your JavaSctipt/jQuery in an external file, you could use the generated ids within your jQuery selectors i.e.
$('#ctl00_ContentPlaceHolder1_Label3').html(xml);
Obviously, this approach requires you to find out what the generated ids will be and requires caution if you ever start changing the site/application construction.
Otherwise, your best options are
1. Use the inline server side code markup. The downside to this approach is that you cannot put your js code in an external file -
$('#<%= Label3.ClientID %>').html(xml);
2. Define unique CSS classes on each control you need to use in your jQuery, which would still allow you to put your js code in an external file -
<asp:Label ID="Label3" runat="server" Text="test" CssClass="label3">
</asp:Label>
$('.label3').html(xml);
3. Use jQuery selectors to pattern match the original id, which again, would allow you to put your js code in an external file -
$('[id$=Label3]').html(xml);
This jQuery selector will select all elements with attribute id whose value ends with Label3. The only potential downside that I could see with this approach is that in theory, it could be possible to have a Label control with id Label3 in say, a Master page and also in two content pages. In this example, using the jQuery selector above would match all three labels, which may have unwanted consequences.
EDIT:
I thought it might be useful to raise your attention to the IDOverride control. An Example page can be found here
It allows you to specify which controls should have their mangled id within the outputted HTML markup overridden with the id as is given in the .aspx file when rendering out the HTML page. I have only played with it briefly with a single Master Page and Panels, but it appears to work well. Using this, you could use the original ids within your jQuery selectors. Be aware however, that the results are unpredictable if you were to have controls with the same ids in your Master page(s) and Content page(s) that are combined to render the HTML for one page.
Short answer, don't worry about using the asp.net IDs. In asp.net you can add your own attribute to a tag:
<asp:TexBox ID="myTBox" runat="server" MyCustomAttr="foo" />
Then in jquery you can refer to this element via:
$("input[MyCustomAttr='foo']")
I do this all the time with jQuery. Hope it helps.
You can call a class name rather than using an ID
For example;
$('.CssClassName'). ...
Or if you type this in the MasterPage's very first line, your control IDs won't change :
ClientIDMode="Static"
For example;
<%# Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="Main_MasterPage" ClientIDMode="Static" %>
The inline code is the correct way to do it. However this will be changing in ASP.NET 4.0. We have spent some time adding a feature that allows full control of the IDs that are generated client side. Below are some resources on the web about this feature.
http://blog.osbornm.com/archive/2009/01/06/asp.net-4.0-clientid-overview-again.aspx
http://channel9.msdn.com/shows/10-4/10-4-Episode-3-ASPNET-WebForms-40/
Most of the suggestions here will work, but test results on jQuery code show that pure ID selectors are by far the fastest. The one I use most often:
$("[id$=origControlId]")
is pretty slow, but the problem isn't too apparent unless the page has many controls and a lot of jQuery.
Since is it fairly painless to assign multiple classes to a control, you could give each one a CSSClass that matches the ID. Since they would then be unique (you'll have to watch repeater type controls that generate multiple controls), you could select by class.
For example:
<asp:Label ID="Label1" CssClass="Label1 SomeOtherClass" runat="server" Text="test">
</asp:Label>
could be selected uniquely by:
$(".Label1")
Which is almost as fast as an ID select.
I had never considered this one:
$('#<%= Label1.ClientID %>')
but I'm going to try it!
You have to pass the control's ClientID to the javascript code (I doubt though that Label1 gets renamed to Label3)
ClientScriptManager.RegisterClientScriptBlock(GetType(), "someKey",
"$('#" + Label1.ClientID + "').html(xml);", true);
You can use the ClientID (just like everyone else said) but the problem with that is that it cannot be used in an external JavaScript File.
So, ideally, instead of using the ID to reference it from jQuery, use a CSS class:
<asp:Label ID="Label1" runat="server" Text="test" CssClass="myclass"></asp:Label>
Then you can reference it like such:
$(".myclass")
You can override the ClientID and the UniqueID as this fella did.
/// <summary>
/// Override to force simple IDs all around
/// </summary>
public override string UniqueID
{
get
{
return this.ID;
}
}
/// <summary>
/// Override to force simple IDs all around
/// </summary>
public override string ClientID
{
get
{
return this.ID;
}
}
You may also create a custom control that inherits from Label that overrides the ID property to be what you want it to be.
This can be achieved by replacing the asp.net id with the original id as the control is rendered.
This is pretty easy and can be donw by creating a set of custom controls.
http://www.bytechaser.com/en/articles/ts8t6k5psp/how-to-display-aspnet-controls-with-clean-id-value.aspx
At the risk of being patently obvious change:
<asp:Label ID="Label1" runat="server" Text="test"></asp:Label>
to
<Label ID="Label1" Text="test"></Label>
You need to put the property ClientIDMode="Static" in your button, this grants the ID will be the same in runtime, it's what you need to get object in Javascript.
you don't need to "prevent" asp.net from changing the control id in order to use jquery, or javascript at all for that matter.
the id that gets rendered on your page is the ClientID property of the control and what you're setting on the control itself is the ID. you can't set the ClientID property, it's generated for you and may or may not be the same as your ID. however, as others have mentioned you can use the ClientID property either in your aspx:
$('<%= Label1.ClientID %>').html()
or by registering the client script in your code behind file using one of the ClientScriptManager methods.
Just get rid of the placeholder. Or select by class or DOM hierarchy. Or even use partial matching on the ID as a last resort. There are many easy, clean ways to select an element in jQuery, we need to wean ourselves away from our old, ugly ASP.NET habits.
And Dreas Grech put the final nail in the coffin. You can't use use external script files with the $('#<%=label1.ClientID %>') type solutions.
Mike
You still can use PlaceHolders and set ClientIDMode in the PlaceHolder tag :
<asp:ContentPlaceHolder ID="BodyContent" runat="server" ClientIDMode="Static">
</asp:ContentPlaceHolder>
None of the contained controls IDs will be altered.
Simple, with overriding the control class. Here is an example with an HtmlGenericControl but you can do it with any ASP.Net control:
public class NoNamingContainerControl : HtmlGenericControl
{
public NoNamingContainerControl(string tag) : base(tag) { }
public override string ClientID
{
get
{
return this.ID;
}
}
}

Resources