ASP.NET ObjectDataSource 自动绑定到中继器 - 可能吗?

发布于 2024-07-04 14:19:49 字数 902 浏览 4 评论 0原文

我有一个 Question 类:

class Question {
    public int QuestionNumber { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

现在我通过 ObjectDataSource 提供这些 ICollection,并使用绑定到 DataSource 的 Repeater 显示它们。 我使用 <%#Eval("Question")%> 显示问题,并使用 TextBox 和 <%#Bind("Answer")%>< /strong> 接受答案。

如果我的 ObjectDataSource 返回三个 Question 对象,那么我的 Repeater 会显示这三个问题,每个问题后面都有一个文本框,以便用户提供答案。

到目前为止效果很好。

现在我想获取用户的响应并将其放回到相关的 Question 类中,然后我将保留该类。

框架肯定应该为我处理所有这些事情吗? 我使用了 Bind 方法,指定了 DataSourceID,在 ObjectDataSource 类中指定了 Update 方法,但似乎没有办法真正启动整个过程。

我尝试在调用 MyDataSource.Update() 后面的代码中添加一个命令按钮,但它尝试调用不带参数的 Update 方法,而不是它期望的 Question 参数。

当然有一种简单的方法可以在很少或没有代码隐藏的情况下实现所有这一切吗?

似乎所有的部分都在那里,但缺少一些胶水将它们粘在一起。

帮助!

安东尼

I have a Question class:

class Question {
    public int QuestionNumber { get; set; }
    public string Question { get; set; }
    public string Answer { get; set; }
}

Now I make an ICollection of these available through an ObjectDataSource, and display them using a Repeater bound to the DataSource. I use <%#Eval("Question")%> to display the Question, and I use a TextBox and <%#Bind("Answer")%> to accept an answer.

If my ObjectDataSource returns three Question objects, then my Repeater displays the three questions with a TextBox following each question for the user to provide an answer.

So far it works great.

Now I want to take the user's response and put it back into the relevant Question classes, which I will then persist.

Surely the framework should take care of all of this for me? I've used the Bind method, I've specified a DataSourceID, I've specified an Update method in my ObjectDataSource class, but there seems no way to actually kickstart the whole thing.

I tried adding a Command button and in the code behind calling MyDataSource.Update(), but it attempts to call my Update method with no parameters, rather than the Question parameter it expects.

Surely there's an easy way to achieve all of this with little or no codebehind?

It seems like all the bits are there, but there's some glue missing to stick them all together.

Help!

Anthony

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

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

发布评论

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

评论(4

囍笑 2024-07-11 14:19:49

那么,如果我必须在回发时手动绑定所有内容,那么 Bind 方法(相对于 Eval 方法)有什么意义呢?

Then what's the point in the Bind method (as opposed to the Eval method) if I have to bind everything back up manually on postback?

写下不归期 2024-07-11 14:19:49

Ben:经过尝试,item.DataItem 始终为 null,并且根据以下帖子,它不是设计用于这种方式的:

http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic4049.aspx

那么到底是怎么回事我要手动将其绑定回来吗?

Ben: Having tried it, item.DataItem is always null, and according to the following post, it's not designed to be used that way:

http://www.netnewsgroups.net/group/microsoft.public.dotnet.framework.aspnet/topic4049.aspx

So how on earth do I manually bind it back?

凶凌 2024-07-11 14:19:49

绑定方法实际上不适用于转发器,它更适用于表单视图或网格视图,在其中您只编辑列表中的一项,而不是列表中的每一项。

在这两个按钮上,您单击编辑按钮,然后为您提供绑定控件(使用绑定绑定),然后单击保存链接,该链接会自动将项目保存回数据源中,而无需隐藏任何代码。

The bind method really isn't for the repeater, it's more for the formview or gridview, where you are editing just one item in the list not every item in the list.

On both you click a edit button which then gives you the bound controls (bound using bind) and then hit the save link which auto saves the item back into your datasource without any code behind.

吐个泡泡 2024-07-11 14:19:49

您必须处理回发事件(单击按钮或其他事件),然后枚举转发器项目,如下所示:

foreach(RepeaterItem item in rptQuestions.Items)
{
   //pull out question
   var question = (Question)item.DataItem;
   question.Answer = ((TextBox)item.FindControl("txtAnswer")).Text;

   question.Save() ?  <--- not sure what you want to do with it
}

You have to handle the postback event (button click or whatever) then enumerate the repeater items like this:

foreach(RepeaterItem item in rptQuestions.Items)
{
   //pull out question
   var question = (Question)item.DataItem;
   question.Answer = ((TextBox)item.FindControl("txtAnswer")).Text;

   question.Save() ?  <--- not sure what you want to do with it
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文