Inline code in ASp.Net menu item - asp.net

Does anybody know if it is the way to set control's child attributes properties by inline code? I mean something like that
<asp:MenuItem Text="text" NavigateUrl='<%# GetItemURL("val") %>' ></asp:MenuItem>
CodeBehind
protected string GetItemURL(string tag)
{
if (string.IsNullOrEmpty(_pageUrl))
_pageUrl = UrlManager.CastQueryString(Request.Url.ToString());
return string.Format("{0}?item={1}", _pageUrl, tag);
}
Neither of approaches work, whatever you use <%#, <%= , Page.DataBind() etc, you get an obstacle.
It would be very ugly to set such properties in code-behind.
I hope the some method allowing to set such properties in code render blocks is available
thanks in advance.

Your binding syntax is correct. You just need to make sure something is binding the parent of the <asp:MenuItem> control. You can even just run this.Page.Databind(); if there isn't a good databinding context already.

Related

Accessing/Setting the property value of an object property of an ASP.NET User Control - Using Decalarative Syntax

I feel like I recall from days gone by an article explaining how to DECLARATIVELY set the value of the property of an object, exposed as the property of an ASP.NET User Control.
My situation is that I have a user control which contains a LinkButton, amongst other things, of course. I would like the consumer of the User Control to be able to set the TEXT value of the link button in the declarative syntax used to implement the User Control.
Here is the User Control (designer)...
<div id="toolbar">
<ASP:LinkButton runat="server" id="btnFirst" />
<ASP:LinkButton runat="server" id="btnSecond" />
<ASP:LinkButton runat="server" id="btnThird" />
<ASP:LinkButton runat="server" id="btnFourth" />
</div>
Here is the property as defined in the code behind of the User Control ...
public partial class Lookuptoolbar: UserControl
{
public LinkButton FourthButton
{
get { return (this.btnFourth); }
}
}
When I include the control in a page I EXPECTED to be able to set the TEXT of my FOURTH button using the following DECLARATIVE syntax ...
<UC:MyControl id="uc1" runat="server" FourthButton_Text="Click Me!"/>
I had read once somewhere, a long time ago, that you could access the properties of an object (exposed as the property of a user/server control) by using the underscore syntax. This is not working for me at all. Is this no longer allowed or am I missing something? IS there ANY way of doing this?
Thank You,
Gary
Ok ... for any who might be interested I think I have found the answer, or at least the beginnings of it. The syntax would be with a HYPHEN and not an underscore. So the correct syntax would be.
<UC:MyControl id="uc1" runat="server" FourthButton-Text="Click Me!"/>
When accessing the "subproperties" of complex types there may be more to it; I haven't gone into it in detail, but the book "Developing Microsoft ASP.NET Server Controls and Components" by Microsoft Press (ISBN 0-7356-1582-9) discusses this on pages 218-222.
If anybody learns anything more I would love to hear about it, otherwise I hope that this helps somebody out there!
-Gary
I have never heard of or seen anybody use the method you talk about (using underscore for accessing object properties in the declarative syntax).
One way to do what you want would be to expose a FourthButtonText property on the user control that interacts with the LinkButton's text property:
public string FourthButtonText
{
get { return this.btnFourth.Text; }
set { this.btnFourth.Text = value; }
}

How can I create an object in .ascx?

I've wrapped the asp:DropDown control so that I can create a lot of instances of it, all of which using the same additional functionality. I want to be able to create this object completely in the .ascx rather than using the code behind. I'm almost there, with the exception of the ListItems.
Here's what I have thus far, can anyone help me to figure out how to get the list items to populate?
<Control:DropDown ID="choice" runat="server" DropDownListLabel="Some Choice:"
QueryString="choice" SelectedIndex="0" ListItems='<%# new ListItemCollection(){
new ListItem("<no filter>", "-1"), new ListItem("Yes", "y"), new ListItem("No", "n")
} %>' />
Everything is working, with the exception of the ListItems, although the list items do work as expected from code behind. Any help on how I can get the ListItems accessor to call properly?
Thanks in advance,
Brett
You probably should have created your control to inherit from System.Web.UI.WebControls.DropDownList then override rendering and add properties that you want extra, this would allow you to something like the following:
<Control:DropDown id="mycontrol" runat="server" DropDownListLabel="Some Choice:" ..>
<asp:ListItem Text="<no filter" value="-1" />
...
</Control:DropDown>
Where DropDownListLabel is an added property. Of course in your case you'd add QueryString as a property as well

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 can I access runat="server" ASP element using javascript?

It seems everyone is doing this (in code posts etc.)...but I don't know how. :(
Whenever I try to manipulate an asp element using JavaScript I get an "element is null" or "document is undefined" etc. error.....
JavaScript works fine usually,...but only when I add the runat="server" attribute does the element seem invisible to my JavaScript.
Any suggestions would be appreciated.
Thanks, Andrew
What's probably happening is that your element/control is within one or more ASP.NET controls which act as naming containers (Master page, ITemplate, Wizard, etc), and that's causing its ID to change.
You can use "view source" in your browser to confirm that's what's happening in the rendered HTML.
If your JavaScript is in the ASPX page, the easiest way to temporarily work around that is to use the element's ClientID property. For example, if you had a control named TextBox1 that you wanted to reference via JS:
var textbox = document.getElementById('<%= TextBox1.ClientID %>');
Making an element runat="server" changes the client-side ID of that element based on what ASP.NET naming containers it's inside of. So if you're using document.getElementById to manipulate the element, you'll need to pass it the new ID generated by .NET. Look into the ClientId property to get that generated ID...you can use it inline in your Javascript like so:
var element = document.getElementById('<%=myControl.ClientID%>');
If you have a textbox:
<asp:TextBox id="txtText" runat="server" />
YOu can use:
var textBox=document.getElementById('<%=txtText.ClientID %>');
Any WebControl exposes the same ClientID property.
All though the question has been answered, thought I would just post some further info...
Rick Strahl provided quite an intresting work around to this problem.
http://www.west-wind.com/WebLog/posts/252178.aspx
Thankfully when ASP .NET 4.0 arrives, it will allow you to specify exacly what the client ID's will be!
http://www.codeproject.com/KB/aspnet/ASP_NET4_0ClientIDFeature.aspx

How do I make my ASP.NET server control take an embedded code block as a property value?

I have a custom server control with a property of Title. When using the control, I'd like to set the value of the title in the aspx page like so:
<cc1:customControl runat="server" Title='<%= PagePropertyValue%>' >
more content
</cc1:customControl>
When I do this, however, I am getting the exact String <%= PagePropertyValue%> being displayed rather than the Property Value that I would like to see.
So after trying the databinding expression (as suggested below). I don't get the string literal that looked bad but I don't get anything else either.
<cc1:customControl runat="server" Title='<%# PagePropertyValue%>' >
more content
</cc1:customControl>
What do I need to do to my custom control to take this sort of value? Or is there something I need to do to the page.
You cant. <%= %> will write the string directly to the response-stream, which happens after the server control is constructed. See this post for an explanation.
So its either codebehind, or <%# + databinding as Zachary suggests.
As a followup to my own question, I have discovered that what I really wanted was to use ASP.NET Expressions using the <%$ syntax, since what I wanted to do was put in localized content.
This can be done with apparently no extra handling on the server control side.
<cc1:customControl runat="server" Title='<%$ Resouces: ResourceFile, ContentKey %>' >
more content and controls
</cc1:customControl>
This works just fine.
Try using databinding syntax:
<%# PagePropertyValue %>
For the bind property value to work correctly as suggested, you will have this in the aspx or ascx file :
<cc1:customControl runat="server" Title='<%# PagePropertyValue %>' >
more content
</cc1:customControl>
You will then need to actually bind data in your page wich you have to add this in you code behind file (code in C#)
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
That way it will bind the data in your ascx or aspx file.
Note that this is specific to control attributes. When using the <%= syntax outside control attributes meaning anywhere else in the page the syntax works as expected. So this
<%=GetCapitalUserName()%>
would call the correct method and inject the result of the call in the page.

Resources