转发器内部动态控件的视图状态问题 [英] problem with viewstate of dynamic controls inside a repeater

查看:312
本文介绍了转发器内部动态控件的视图状态问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在使用添加了动态控件的中继器时遇到了一个问题,尽管我有一种变通方法可以在功能上完全实现我想要的功能,但我想知道是否有更好的解决方案我理解的方式.我已经使用ASP.NET大约6个月了,每次我认为页面生命周期/视图状态都完全怀疑了我无法回答的问题.

I ran into a problem recently when using a repeater that I was adding dynamic controls into and although I've got a workaround that does functionally exactly what I want it to do, I'd like to know if there is a better way to do it for my understanding. I've been working with ASP.NET for about 6 months now, and everytime I think I've got the page lifecycle/viewstate completely sussed something crops up that I can't answer.

我正在创建一个表单,用户可以在该表单中注册对事件的兴趣,并可以注册多个人.

I was creating a form where users could register their interest for an event, and register for multiple people.

aspx类似于:

<asp:Repeater ...>
    <bunch of formatting and always there controls like firstname/lastname/address>
    <asp:PlaceHolder ...>
        <dynamic controls for workshop selection go here>
    </asp:PlaceHolder>
</asp:Repeater>

活动可以有用户可以注册的研讨会,并且研讨会的可用性取决于他们选择参加活动的日期.讲习班的可用性取决于日期,因此他们在选择日期之前不能选择讲习班.

An event can have workshops that the user can register for, and the availability of the workshops is dependant on the date that they choose to go to the event on. The availability of the workshops is dependent on the date, so they can't choose the workshops until they've selected a date.

无论如何,我要添加的动态控件基本上是一堆文字和一堆单选按钮组.

Anyway the dynamic controls that I'm adding are basically a bunch of literals and a bunch of radio button groups.

我首先在ItemDataBound事件处理程序中添加了控件,但是当将转发器项目保存回我的委托人列表时,单选按钮的ViewState从未更新.在ItemTemplate中引用的所有字段都可以很好地处理,但不是我动态添加的单选按钮.

I started off by adding the controls in the ItemDataBound event handler, but when saving my repeater items back to my delegate list the ViewState of the radio buttons was never updated. All the fields that were referenced in the ItemTemplate were handled fine, but not the radio buttons I was adding dynamically.

我尝试改用ItemCreated事件,在那里添加我的按钮,但这似乎没有任何区别.最后,我确定了一种基于.基本上,我只是以文字形式输出HTML输入字段,然后从请求中读取它们.

I tried to use the ItemCreated event instead, adding my buttons there, but that didn't seem to make any difference. In the end I settled on a workaround which was based off of this. Basically I'm just outputing the HTML input fields in a literal and reading them back from the request.

现在所有功能都可以完美运行,并且我对功能感到满意,不得不直接将单选按钮输出为HTML输入而不是使用服务器端控件似乎很肮脏.有没有人知道为什么ViewState无法正确还原?

It all works perfectly now, and I'm happy with the functionality, it just seems really dirty to have to output the radio buttons as HTML inputs directly rather than use a server side control. Does anyone have a clue about why the ViewState wasn't being restored properly?

请注意,每次都以相同的顺序重新创建控件,并正确设置了ID.我到处都在使用动态控件,它们工作正常,当我将它们添加到中继器中时,我无法使它们在这种情况下正常工作.

Just to be clear, the controls were recreated in the same order everytime, with the ID properly set. I'm using dynamic controls all over the place and they're working fine, I just can't get them to work in this case when I'm adding them inside a repeater.

//更多说明

我无法在Page.Init中创建这些控件,因为用户选择了导致回发的日期,因此在创建动态控件之前,我必须等待该控件的视图状态加载.

I can't create these controls in Page.Init as the user selects the date which causes a postback and I have to wait for the viewstate of that control to load before I create the dynamic controls.

推荐答案

当转发器绑定到其数据源时,您正在创建(或重新创建)控件.如果在页面加载ViewState之后发生这种情况,则动态创建的控件将无法使用ViewState.

You're creating (or re-creating) the controls when the repeater is bound to its data source. If this happens after the ViewState has been loaded by the page, the ViewState won't be available to the dynamically-created controls.

检查您是否足够早地绑定了中继器-Page.Init正常;Page.Load为时已晚.

Check you're binding your repeater early enough - Page.Init is OK; Page.Load is too late.

这篇关于转发器内部动态控件的视图状态问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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