8

so for two-way (bi-directional) databinding in ASP, we do this...

<asp:textbox id="txtField" runat="server" 
    text='<%# Bind("SomeField") %>'>
</asp:textbox>

SomeField is located on the DataSource of the DetailsView that serves as the container for the textbox.

Alternatively I could do this from code-behind (using the textbox's OnDataBinding event):

protected void SomeField_OnDataBinding(object sender, EventArgs e)
{ 
  ((TextBox)sender).Text = Eval("SomeField").ToString();
}

However, EVAL is read-only...how can I specify Bind (two-way) from code-behind?

2 Answers 2

3

I've managed to find a work-around for my "edge-case".

I am using LLBLGen subtypes and therefore need to switch the datasource of the detailsview based on a radiobutton filter selected by the user.

I tried to bind to the sub type field "declaratively" in ASP using <%# Bind(... This did not work.

I had to resolve to a code-behind "hack" where I conditionally display the controls in the detailsview using details_view pre-render method.

For each field I then conditionally setup it's one-way (read-only) bind in OnDataBinding...

e.g. ((TextBox)sender).Text = Eval("FilePrefix").ToString();

Finaly to get the data to push into the datasource, I hack the DetailsView OnItemInserting/Updating events (conditionally as well)...

e.Values["FilePrefix"] = txtFilePrefix.Text;

I feel so dirty after this hack I think I need a shower...

I still hope someone can provide a cleaner approach :-)

Sign up to request clarification or add additional context in comments.

Comments

0

Did you check out this page on MSDN, Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control ?

3 Comments

The Eval (one-way) method works. I want Bind (two-way) to work. Also I am not using a repeater, but a detailsview.
I think I'm probably misunderstanding your problem. There's really no such thing as two-way binding. You can set the value of the control in either the .aspx page or in the code-behind. To get the value back (i.e. the user input), you have to access the Text property of the control. What is "SomeField". A property on the code-behind class?
Somefield if a table column located on the DataSource of the DetailsView that serves as the container for the textbox. I am interested in having the textbox push data into database. Thats why I need to use Bind. It's simple really. Except it does not seem to be easily made available from codebehind. Using <%# Bind( yes, but not as easy in code...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.