tags:

views:

972

answers:

3

Hi!

I'm trying to separate my presentation and logic as much as I can, but there's one problem.

How can i set the text-property to a dynamic value from my design file? I want to do like this:

<asp:Textbox id="txtUrl" runat="server" Text="<%= user.URL %>" />

But this will fail. Am i forced to do this from code behind?

A: 

You can use binding instead of evaluation.

This code binds a text box's Text property to a user's Url property returned by MyData.GetLoggedInUser(). This allows for 2-way binding.

<asp:FormView ID="UserView" runat="server" DataSourceID="LoggedInUser">
    <ItemTemplate>
        <asp:TextBox ID="tb" 
                     runat="server" 
                     Text='<%# Bind("Url") %>'></asp:TextBox>
    </ItemTemplate>
</asp:FormView>
<asp:ObjectDataSource ID="LoggedInUser" 
                      runat="server" 
                      SelectMethod="GetLoggedInUser" 
                      TypeName="MyData">
</asp:ObjectDataSource>
Frank Krueger
Well, the output I get is <input name="txtURL" type="text" value="<%= user.URL %>" id="txtURL" />. How would I do the binding solution?
alexn
A: 

How about this :

<input type="text" 
  id="txtUrl" name="txtUrl" runat="server" 
  value='<%= user.URL %>' />
Canavar
+1  A: 
<asp:Textbox id="txtUrl" runat="server" Text="<%# user.URL %>" />

It's all about the #. but it won't get set till txtUrl.DataBind() or something higher in the object heirarchy (like the Page) calls DataBind().

Al W
Thanks, this solution works great!
alexn