How to bind a classes property to a TextBox? - c#

I have a Customer class with a string property comments and I am trying to bind it like this:
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text=<%=customer.Comments %>>
</asp:TextBox>
However, it gives me the error:
Server tags cannot contain <% ... %> constructs.
I also have a method in the class called GetCreatedDate and in the aspx page, I am doing
<%=GetCreatedDate()%> and <%GetCreatedDate();%>. What is the difference?

Alternatively you can set the value in the Page_Load event of the code behind file:
txtComments.Text = customer.Comments;

you should use "<%# %>" for data binding
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text="<%# customer.Comments %>">
</asp:TextBox>

Try this instead.
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text=<%# customer.Comments %>>
</asp:TextBox>
Notice the = to #

Use the DataBinding syntax as stated, <%# customer.Comments %>. This syntax is only evaluated when the TextBox is databound. You would usually use it in a DataBound list. In this case you need to databind the control manually. Override the page's OnDataBinding method and call txtComments.DataBind();
The databinding syntax is the only way to declaratively set ServerControl properties from the aspx page. The Response.Write of the other syntax happens at a time that the ServerControl properties cannot be accessed. If the control is not inside a databound control, you have to databind it.
If you were looking to go all declarative in your page, you don't gain to much using this method because you still need to write code in the code behind.
An alternative if you want to use the TextBox on its own without a parent DataBound control would be to subclass the TextBox, add an AutoBind property, and in the subclassed control call its DataBind method if it is true. That would let you bind the values without writing databinding code in the code behind.
You could also add the TextBox and other form controls to a FormView control and bind it to your object. You can still use the DataBinding syntax in that case.

try this
<asp:TextBox ID="txtComments"
runat="server"
TextMode="MultiLine" Text='<%# customer.Comments %>'>
</asp:TextBox>

Related

Validation in repeater causes strange error

I have a repeater in which i have a textbox with a regex validator. The code looks basicly like this:
<asp:Repeater ID="rptr" runat="server">
<ItemTemplate>
<div>
<!-- controlls -->
<asp:TextBox ID="tbText" runat="server" ClientIDMode = "Static" EnableViewState="true" OnTextChanged="tbText_TextChanged"></asp:TextBox>
<asp:RegularExpressionValidator ID="tbTextValidation" runat="server" Text="*" ToolTip="wrong!" ControlToValidate="tbText"
SetFocusOnError="false" ValidationExpression="^([a-zA-Z])$">
</asp:RegularExpressionValidator>
</div>
</ItemTemplate>
</asp:Repeater>
The problem I have is that when I add a wrong text in the first field of the repeater the error text appears on all textBoxes (even if the rest are correct)
If i add a correct text in first field and a wrong text in another field/fields, the error text appears only where it's supposed to(in the wrong fields).
So, my question is, what could cause this?
Please try after removing the ClientIDMode = "Static" from text box of the repeater

ASP.Net Repeater Eval in an If statement

So I've been looking around for a good answer to this question, but haven't really found anything useful. Hopefully someone can shed some light on this for me.
Basically, I have a repeater that is backed by a database table. Inside the ItemTemplate for this repeater, I have some HTML elements that are populated with properties from each item in the list. Pretty standard stuff. However, there is a possibility that one of the items could be null. In that case, it would make sense for me to put some sort of if (blah != null) logic around the offending code. The only problem is, when I've tried to do so, ASP throws up on me, telling me that I can't use an if statement inside of <%# %>.
My question to the masses is this: if you can't use an if statement inside of <%# %>, then how are you supposed to do conditional logic based on the values of each item?
I know that you can call your own methods inside the repeater, but that won't work for what I'm trying to do.
Below is what I'm trying to accomplish, to better illustrate my point.
<asp:Repeater runat="server" ID="repeater">
<ItemTemplate>
<div class="item-wrap">
<% if(Eval("imageUrl") != null) { %>
<div class="plan-img">
<asp:Image runat="server" ImageUrl='<%# Eval("imageUrl") %>'/>
</div>
<% } %>
</div>
</ItemTemplate>
</asp:Repeater>
inside your ItemTemplate write the markup like this:
<asp:Panel runat="server" Visible='<%# Eval("imageUrl") != null %>'>
<asp:Image runat="server" ImageUrl='<%# Eval("imageUrl") %>'/>
</asp:Panel>
Basically you can't mix code <% with databinding constructs <%#.
My advice would be to add the following property in your CodeBehind:
protected YourClass DataItem
{
get
{
return (YourClass)this.Page.GetDataItem();
}
}
and then write the markup without Eval():
<asp:Image runat="server" ImageUrl='<%# DataItem.imageUrl %>'/>
You're supposed to generate the same content for every item in the template. If you don't need to use it for a particular item just set it's visibility to false in the binding events.

Dynamic data in Repeater

in my webpage different users can visit same profile, but users can be ither Owner of a profile, or Visitors, and content have to change for those two conditions.
if visitor check profile he see this editor:
<asp:Repeater id="UserLoginRepeater" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server">label Text</asp:Label> <asp:Label runat="server">label text 2</asp:Label>
</br>
<asp:Button runat="server" Text="TestButton"/>
</br>
<asp:Label ID="Label3" runat="server">Test</asp:Label> <textarea><%# Eval("Content") %></textarea>
</br>
</br>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
codebehinde:
if User is Owner of a profile TextArea have to be replaced by a label and an other label with textbox have to be added.
I found couple of possible implementations of this on internet, but they are not so easy to maintain. for example
http://tinyurl.com/9764eys
What would be the best way to Load controls dynamicly into Repeater?
Use a <asp:ContentPlaceholder> then in your code behind add a handler for the OnItemDataBound event. If you want those controls to handle events, make sure you bind your repeater before OnPreRender for the page.
Why not just create two repeaters for two conditions?
Just bind the repeater for the specific user and hide the other one.
Made Dynamic load of Controllers in code-behinde.

c# code in control attribute in aspx file

I'd like to change the text attribute of a button according to the session landuage. So I write:
<asp:Button id="btOptIn" runat="server"
Text="<% = UI.Instance.TxtSubmit %>"
onclick="btOptIn_Click" />
This does not work. I tried several other versions (like Text='<%# Eval(UI.Instance.TxtSubmit) %>') but could not succeed.
The same code (<% = UI.Instance.TxtSubmit %>) works outside the quotes of the attribute. What is the syntax to make it work within an attribute of a control?
Thank you for your time.
<asp:textbox id="tbName" runat="server" Text='<%# Eval("test") %>' />
<%= %> is a shortened response.Write() and is never valid as an attribute, for any server tag.
<%# %> can be used, only if the conatainer is databound (the page in your case).
<%$ > can be used to access data in resources files.
In the Page_Load of you will have to make a call to Page.DataBind() for this to work.

Accessing code behind property in server tags

Is it possible to access a code behind property for a server tag?
I have a property on my code behind page that i want to pass into a javascript function i.e.
<asp:RadioButton onclick="moveToNextPage()" class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
So i want to be able to get the variable out of my code behind page and pass it to the "moveToNextPage()" function. Is this possible?
If your code behind value is public or protected then you can try this (assuming your variable is called example):
<asp:RadioButton onclick='<%= string.Format("moveToNextPage({0})", example) %>' class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
<asp:RadioButton onclick='<%= string.Format("moveToNextPage({0})", yourproperty) %>' class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
Make sure your yourproperty is public
<asp:RadioButton onclick='<%# string.Format("moveToNextPage({0})", yourproperty) %>' class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
Important change would be <%# instead of <%=. In order for this to work, you need to call myRadioButton.DataBind() somewhere in code behind

Categories