有没有办法在网络表单中绑定文本框,以便在用户输入时保存数据?

发布于 2024-07-26 07:41:24 字数 749 浏览 13 评论 0原文

我的网络表单中有许多 TB 绑定到后面代码中的一个属性:

<asp:TextBox ID="tbFirstName" Text="<%# Contact.FirstName %>" runat="server" />
<script language="c#">
    public Contact Contact
    {
        get
        {
            return (Contact)ViewState["Contact"];
        }
    }
</script>
<script language="VB">
    Public ReadOnly Property Contact() As Contact
        Get
            return ViewState("Contact");
        End Get
    End Property
 </script>

而“联系人”就是该属性。

我希望当用户插入文本时,它应该立即绑定到 Contact 对象,例如当用户按下某个键时或者甚至在失去焦点时(TextChanged)也很好。

有没有办法动态地执行此操作(而不是手动从所有 TB 检索数据并更新联系人对象)?

我实际上愿意通过在表单主体中传播的简单文本框来实现双向数据绑定。

注意:我当然不会将项目存储到数据库中,我只想要驻留在状态管理器中的对象(联系人)。

I have in my webform many TBs bound to a property in the code behind:

<asp:TextBox ID="tbFirstName" Text="<%# Contact.FirstName %>" runat="server" />
<script language="c#">
    public Contact Contact
    {
        get
        {
            return (Contact)ViewState["Contact"];
        }
    }
</script>
<script language="VB">
    Public ReadOnly Property Contact() As Contact
        Get
            return ViewState("Contact");
        End Get
    End Property
 </script>

While Contact is that property.

I want that when the user inserts text, it should immediately be bound to the Contact object e.g. when the user presses a key down or even when losing focus (TextChanged) would be good too.

Is there a way to do it dynamically (rather than manually retrieve the data from all the TBs and update the Contact object)?

I am actually willing to achieve two-way databinding with simple textboxes spread in the form body.

Note: I am not going to store the items to the DB of course, I just want the object (Contact) which resides in the state manager.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隱形的亼 2024-08-02 07:41:25

您知道您正在谈论 Web 应用程序吗? 它在用户的浏览器中运行。 为了更新数据库,您必须通过 AJAX 或回发来往返服务器。 您真的想每次击键都执行此操作吗?


从您的评论中可以看出,您显然并没有尝试在每次击键时都写回数据库。

不过,数据绑定并不是这样工作的。 数据绑定是 ASP.NET 中纯粹的服务器端操作。 即使 Bind 方法提供的双向数据绑定也只能在完整回发时起作用(尽管我承认我还没有使用 UpdatePanel 尝试过)。

作为实验,创建一个新页面,并设置双向数据绑定(请参阅 插入、更新和删除数据概述作为示例)。 一旦你让它“正常”工作,尝试将 FormView 放入 UpdatePanel 中,看看 Bind 是否仍然有效。 如果是这样,请查看是否可以让 UpdatePanel 在每次击键时触发。

Do you realize you're talking about a web application? It's running in the users' browser. In order to update a database, you have to make a round trip to the server, either through AJAX or through a postback. Do you really want to do this for every keystroke?


From your comments, it's apparent that you aren't trying to write back to the database on every keystroke.

Still, data binding doesn't work this way. Data binding is a purely server-side action in ASP.NET. Even the two-way data binding afforded by the Bind method only works on a full postback (though I admit I haven't tried it with an UpdatePanel).

As an experiment, create a new page, and set up two-way databinding (see "Using the FormView for a More Flexible Data Modification User Interface" in An Overview of Inserting, Updating, and Deleting Data for an example). Once you get it working "normally", try putting the FormView into an UpdatePanel and see if the Bind still works. If so, see if you can get the UpdatePanel to fire on every keystroke.

甜味拾荒者 2024-08-02 07:41:25

不要再像开发桌面应用程序一样思考! 因为你不是。 “联系人”对象位于服务器上,而文本框“位于”客户端,刷新服务器对象的成本非常高,您必须使用新数据在服务器和客户端之间进行异步传输,然后执行此操作如此短的间隔甚至是不可能的。 认为您可以在文本框上添加延迟,然后将数据传输到服务器。 你为什么需要这个?

Stop thinking like you are developing a desktop application! Because yo are not. The "Contact" object lives on the server while your textbox "lives" at the client, refreshing the server object would be very costly, you'll have to do async transfers between the server and the client with the new data, and doing it at so shorts intervals wouldn't even be possible. Thought you can add a delay on the textbox after which you would transfer the data to the server. Why would you ever need this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文