Custom ASP binding mechanism problem - asp.net

know ASP since about 6 months, and I've go hard problem. I've created my own DataBinding mechanism and I need some solution to do somthing like this:
I have this example code in my ASPX
<myButton id="someID" runat="server" Text="SomeText" BackColor="{SomeContext}" />
and I have in this control my property Context of type Object. I want to bind property which is inside Context to BackColor property. Name of the property from Context is SomeContext (in brackets). All I need is to use some TypeConverter or other technology to identify that there is a name inside {} and remember that name inside instance of myButton control. Any ideas???
I thought that I can use my own class inherited from TypeConverter and catch the moment of converting value {SomeContext} to Color (the BackColor property). I can catch this moment, but I have no info about target control, only empty context of String value. If anyone know how to get target property somehow, will be very!! helpful.
I've been searching web, and nothing...

You can set this in your code behind directly:
someID.BackColor = context.Color;

Ya might give ASP.NET Custom Expression builders a try.

I won't use codebehind in my project. Website will be rendered from compiled customized aspx from database. I have special approach for my system, and my binding is a main part of it. Regular databinding expression won't help because it use ASP binding, which I don't want to use.

Unfortunately I didn't find any solution of my problem, so I decided to use inner properties. I'll use ASPX code like this:
<myButton runat="server" ID="someID">
<Binding>
<Bind Target="BackColor" Source="ColorOfBackgroundFromContext" />
<Bind Target="ForColor" Source="ColorOfForegroundFromContext" />
</Binding>
</myButton>

Related

Passing dynamic variable to UserControl attribute

I'm creating an UserControl and I'm passing a variable to it.
It works fine if I do it this way:
<uc:TestControl ID="testControl" runat="server" Variable="test"></uc:TestControl>
However I want to pass a dynamic variable to the control like this:
<uc:TestControl ID="testControl" runat="server" Variable="<%=dynamicVariable%>"></uc:TestControl>
But unfortunately that doesn't work and I know I could assign it on Page_Load but I don't like that way.
So I'm wondering if it's possible at all. Is there any way to assign a dynamic variable to an attribute like I wanted above? Or am I required to do it in Page_Load?
Any feedback would be appreciated!
Thanks!
<%= syntax doesn't work with controls marked as runat="server" .Try using the databind syntax
Variable="<%#dynamicVariable%>"
Then calling databind on the user control as per this SO question
EDIT
To database i think it's a straight
testControl.DataBind()
supported in ASP.Net 3.5 and greater.

How can I use HTML5 email input type with server-side .NET

As I understand it, the <input type=email> element in HTML5 will render as a simple text field in browsers that do not support the tag. On other browsers it will render properly, like on the iPhone it will bring up the e-mail keyboard layout.
I’d like to use this in a project but my input fields are <asp:TextBox> controls. How can I use the HTML5 element but still access its data server-side like the rest of my fields?
There is an update for .NET framework 4 which allows you to specify the type attribute
http://support.microsoft.com/kb/2468871.
See feature 3 way down the page
Feature 3
New syntax lets you define a
TextBox control that is HTML5
compatible. For example, the following
code defines a TextBox control that is
HTML5 compatible:
<asp:TextBox runat="server" type="some-HTML5-type" />
you can try adding the attributes manually, like:
TextBox1.Attributes["type"] = "email";
TextBox1.Attributes["type"] = "url";
TextBox1.Attributes["type"] = "number";
Sorry I'm a bit late to the party, though I think that others can benefit from what I did. I have a page which is HTML 5 though we still have .NET 3.5. We wanted to keep the .NET element, though have the type change to email. I've tried several methods (including Milox above) to no avail, though the one which worked for me was the following: I added a JavaScript property to the element itself inline (when I put it in a script tag it wouldn't pick up for some reason...)
Here is what your tag would look like if you use my changes:
<asp:TextBox runat="server" type="email" onfocus="this.type='email'"/>
Eli
Whether or not it is accessible as a server control, you should be able to access the HttpRequest.Form collection and retrieve the value. No matter what the browser does with the tag, it has to submit a string to the server.
in your .aspx file add
<input type="text" required autofocus placeholder="Email Address"
class="txt-input txt-input-username" ID="myTextBox" runat="server"/>
in your Code Behind .cs
myTextBox.Attributes["type"] = "email";
This Worked For Me
You need to create your own custom control and override the Render routines. Feel free to use either the source code or DLLs

Explicit localization problem

when trying to translate the confirmation message to Norwegian i get the following error:
Cannot have more than one binding on property 'OnClientClick' on 'System.Web.UI.WebControls.LinkButton'. Ensure that this property is not bound through an implicit expression, for example, using meta:resourcekey.
i use Explicit localization in the following manner:
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="<%# Resources: lnkMarkInvoicedResource.OnClientClick%>"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
here's the local resource file entry:
<data name="lnkMarkInvoicedResource.OnClientClick" xml:space="preserve">
<value>return confirm('Er du sikker?');</value>
if i remove the meta attribute i get the English text(default).
how do i get the Norwegian text appearing without resorting to using the code behind?
Update:
removing the meta attribute prevents the exception from occurring but the original problem still exists. I can't get the Norwegian text to show.
only the default English text shows.
Another Update:
I know this question is getting old but i still can't get the Norwegian text to display.
If anyone has some tips please post a response.
Looks like you're making the problem harder by inlining the onclick. Why not split it out to a separate line?
<script>
function markInvoiced()
{
return confirm('<%= Resources.SomehowGet.lnkMarkInvoicedResource.OnClientClick%>');
}
</script>
<asp:LinkButton ID="lnkMarkInvoiced" runat="server" OnClick="lnkMarkInvoiced_OnClick"
OnClientClick="return markInvoiced();"
Visible="False" CssClass="stdtext" meta:resourcekey="lnkMarkInvoicedResource" ></asp:LinkButton>
And while we're looking at your code, you realize that you're essentially building an <a> tag, right? As such, why not just build an <a> and save yourself some grief?
And finally, next project why not ditch the built-in ASP.NET localization nighmare in favor of something sane like FairlyLocal, in which case you'd write this:
<a href="#" onclick="return confirm(<%=_"really?"%>) onserverclick="lnkMarkInvoiced_OnClick" runat="server">
<%=_("Mark Invoice")%>
</a>
Are you using the .NET resource manager and satellite assemblies to store your localized resources? It looks like you have hard-coded the alternative language in your markup, rather than storing it in a language-specific resources assembly...
.NET has some extremely rich localization and globalization capabilities. If you use them properly, localization should be a pretty automatic thing (assuming your client is providing their language code as part of the HTTP headers). Even if your client has not configured their browser with the appropriate language, it is still easy enough to manually change the UI culture via a user request (clicking a flag icon, configuring a setting, etc.)
This article might be helpful: ASP.NET Web Page Resources Overview
That meta tag is using implicit localization when you're using explicit localization in the OnClientClick. You will need to choose one method or the other. If you are to use explicit localization, you'll need to do the necessary work to set the proper culture info in your application in the code-behind.

How to construct a control like the Asp.Net ScriptManager

Maybe this is not the best title to it, but this is what I am trying to find out. I need a way to construct a control in ASP.Net that mimics the ScriptManager in that it should look like this:
<uc:MyControl Id="" ...>
<MyListOfItems>
<uc:Item someProperty="value1" someOtherProperty="value2" />
<uc:Item someProperty="value3" someOtherProperty="value4" />
</MyListOfItems>
</uc:MyControl>
How to make the Control have a List of items that you can add items to from the Source of a page.
Thanks in advance for your help.
Basically you need to instruct your control to parse any inner content as subcontrols. This is done using a couple attributes - ParseChildrenAttribute and PersistChildrenAttribute (if you want child controls to be maintained in ViewState).
Create a custom control that inherits from WebControl.
Add a [ParseChildren(true, "MyListOfItems")] attribute to your class.
Create a MyListOfItems property.

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

Resources