Databinding in ASP.NET - asp.net

Is it possible to do data binding on the fields of an object just like in WPF?
Something like this:
<TextBlock Text="{Binding ElementName=lbColor}" />
just for an asp:TextBox element. What I want is that on submit to have the new values inside the binded object and I don't want to manually read and set the values.

if you are binding in a template you can use:
<asp:textbox Text="<%#Bind("lbColor")%>" runat="server" />
see: http://support.microsoft.com/kb/307860

For those looking, I made this javascript plugin. It does data binding very close to what WPF does and is easy to use. It has attached documentation in a .txt file
https://github.com/jdemeuse1204/ObjectDataBinding
Here is an example of what one of my bindings looks like
<input runat="server" style="width: 50px;" data-bind="path: Quote" />
To activate you call
$("#yourelementname").observe(yourobject, 'yourbindingname');
To get the object back for saving or other operations
var obj= $("#yourelementname").getObserveObject('yourbindingname');

Related

ASP.NET: Can I read the ViewState to restore a control in the client site?

I am curious: is it possible to read the initial state of a DropDownList control using JavaScript?
Let’s say that when the page is loaded in the browser, the dropdown has ten options. Then, using JavaScript I remove all the options.
Can I read the ASP.NET ViewState to get the initial ten options and restore them?
The short answer is yes you can use JavaScript to read the viewstate values as they are stored in a field called __viewstate, which is rendered in the browser as an input field like this:
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="..." />
The problem you will run into is the __viewstate value is encrypted.
My suggestion is to use a hidden field to store the values of the dropdown or whatever else you want to store and then access the value like this:
<input type="hidden" id="hiddenField" runat="server" value="" />
Then in your code-behind, since the input has the runat="server" you can set the value to whatever you wish, like this:
hiddenField.Value= ViewState["dropdownvalues"].ToString();
Finally, you can use JavaScript to get the values from the hidden field, like this:
<script type="text/javascript">
function test()
{
var name = document.getElementById('hiddenField').value;
alert(name)
}
</script>

How do I put hint in a asp:textbox

How do I put a hint/placeholder inside a asp:TextBox? When I say a hint I mean some text which disappears when the user clicks on it. Is there a way to achieve the same using html / css?
The placeholder attribute
You're looking for the placeholder attribute. Use it like any other attribute inside your ASP.net control:
<asp:textbox id="txtWithHint" placeholder="hint" runat="server"/>
Don't bother about your IDE (i.e. Visual Studio) maybe not knowing the attribute. Attributes which are not registered with ASP.net are passed through and rendered as is. So the above code (basically) renders to:
<input type="text" placeholder="hint"/>
Using placeholder in resources
A fine way of applying the hint to the control is using resources. This way you may have localized hints. Let's say you have an index.aspx file, your App_LocalResources/index.aspx.resx file contains
<data name="WithHint.placeholder">
<value>hint</value>
</data>
and your control looks like
<asp:textbox id="txtWithHint" meta:resourcekey="WithHint" runat="server"/>
the rendered result will look the same as the one in the chapter above.
Add attribute in code behind
Like any other attribute you can add the placeholder to the AttributeCollection:
txtWithHint.Attributes.Add("placeholder", "hint");
Just write like this:
<asp:TextBox ID="TextBox1" runat="server" placeholder="hi test"></asp:TextBox>
<asp:TextBox runat="server" ID="txtPassword" placeholder="Password">
This will work you might some time feel that it is not working due to Intellisence not showing placeholder
Adding placeholder attributes from code-behind:
txtFilterTerm.Attributes.Add("placeholder", "Filter" + Filter.Name);
Or
txtFilterTerm.Attributes["placeholder"] = "Filter" + Filter.Name;
Adding placeholder attributes from aspx Page
<asp:TextBox type="text" runat="server" id="txtFilterTerm" placeholder="Filter" />
Or
<input type="text" id="txtFilterTerm" placeholder="Filter"/>
asp:TextBox ID="txtName" placeholder="any text here"

Post data to another aspx page

I have simple aspx page that has a form and a input field in it something like below.
//basket.aspx
<input type="text" id="TotalPrice" runat="server" name="TotalPrice" value="100" />
<asp:button ID="btnBuy" runat="server" Text="make payment" PostBackUrl="~/payment" />
</form>
This is going to payment.aspx but when I debug the Request.Form in the page load of the payment.aspx, I cannot reach the TotalPrice value like Request.Form["TotalPrice"]. What is the best way to make a post to another aspx page to get values ? Why the way I am trying to does not work ?
You should be using PreviousPage property on the target page.
Here is an article on how to make cross post backs work

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

Adding custom attributes to asp.NET RadioButton control

I want to add a custom attribute to an asp.net RadioButton called Key which I'm using client-side for an ajax request.
What I'm finding is that my aspx markup which is the following:
<asp:RadioButton ID="rdoPost" GroupName=PreferredContactMethod" value="Post" onclick="DoStuff(this)" runat="server" />
gets rendered in the page as
<span Key="ContactMethod">
<input id="rdoPost" type="radio" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
</span>
whereas I'd expected (and hoped) to get the following
<input id="rdoPost" type="radio" Key="ContactMethod" name="PreferredContactMethod"" value="Post" onclick="DoStuff(this);" />
I've tried the same thing with an asp TextBox control and it works exactly as I'd expect simply adding the Key="myKey" attribute to the <input type="text"/> element.
Is there a way around this with the standard RadioButton control, or will I have to inherit from the standard one to achieve the markup I'm wanting?
Also... (sorry to ask two questions at the same time), is adding non-standard attributes to html markup a bad idea anyway? Currently I'm using these attributes in JavaScript in the following way:
var key = rdoPost.Key;
I've found from the question/answer below that the easiest way to do this is via the code-behind using the InputAttributes property as follows:
rdoPost.InputAttributes.Add("class", "myCheckBoxClass");
Why does ASP.Net RadioButton and CheckBox render inside a Span?

Resources