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

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

问题描述

所以对于 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 的 DataSource 上.

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

或者,我可以从代码隐藏中执行此操作(使用文本框的 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 是只读的...如何从代码隐藏中指定绑定(双向)?

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.

我尝试使用 <%# Bind(...这不起作用.

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

我不得不解决代码隐藏的hack"问题,我使用 details_view 预渲染方法有条件地在 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();

最后为了将数据推送到数据源中,我破解了 DetailsView OnItemInserting/Updating 事件(也有条件地)...

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 数据从后面的代码双向绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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