ASP.NET数据绑定从code后面双向,双向 [英] ASP.NET data bind two-way, bi-directional from code behind

查看:129
本文介绍了ASP.NET数据绑定从code后面双向,双向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以对于ASP双向(双向)数据绑定,我们这样做......

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

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

SomeField位于DetailsView控件中,可作为文本框容器中的数据源。

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

我也可以从$ C $做到这一点的C-背后(使用文本框的OnDataBinding事​​件):

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();
}

不过,EVAL是只读的...我怎样才能从code-后面指定绑定(双向)?

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

推荐答案

我设法找到-各地工作,为我的边缘案一个。

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

我使用LLBLGEN亚型,因此需要切换DetailsView控件的基于用户选择的单选按钮过滤器的数据源。

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

我试着用&LT绑定到子类型字段声明中的ASP;%#绑定(...
这并没有工作。

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

我不得不解析为code-背后黑客在这里我使用details_view pre-render方法有条件地显示在DetailsView控件。

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

对于每个字段然后我有条件地设置它是单向的(只读)在OnDataBinding ...

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获得的数据推入数据源,我砍在DetailsView OnItemInserting /更新事件(有条件的为好)...

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 :-)

这篇关于ASP.NET数据绑定从code后面双向,双向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆