回发后如何访问数据列表中的数据绑定选定项?
回发后如何访问数据绑定项?
我有一个数据列表,当用户选择一个项目时,OnItemCommand 事件会触发,我的事件处理程序看起来像这样:
protected void dlResults_Select(object sender, DataListCommandEventArgs e)
{
MyItem item = e.Item.DataItem as MyItem;
}
项目始终为空。有没有办法访问数据绑定项?
How do you access the databound item after postback?
I have a datalist,and when the user selects an item, the OnItemCommand event fires, and my event handler looks liek this:
protected void dlResults_Select(object sender, DataListCommandEventArgs e)
{
MyItem item = e.Item.DataItem as MyItem;
}
item is always null. Is there a way to access the databound item?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
仅当通过 ItemDataBound 事件访问时,DataItem 属性才不为 null。如果您需要 ID 来修改对象/记录,您可以设置 DataKeyField 属性用于填充 DataKeys 集合。
然后,您可以在 OnItemCommand 事件中使用 id 来实例化所需的对象,或作为数据库查询的参数。
The DataItem property is only not null when accessed through the ItemDataBound event. If you require an ID to modify your object/record you can set the DataKeyField property of the DataList to populate the DataKeys collection.
You can then use the id in your OnItemCommand event to instantiate the desired object, or as a parameter to a database query.
这里有一些建议。
A few suggestions here.
我发现解决此类问题的最简单方法是向控件添加一个 javascript 事件,该事件用控件的选定值更新输入控件(type=hidden)。如果将 runat=server 属性添加到标记,您可以访问输入控制服务器端并从中读取值。回发应该在 javascript 事件之后触发,因此其他一切都应该“正常”为您工作。
The simplest approach I have found to this type of problem is to add a javascript event to the control which updates an input control (type=hidden) with the selected value of the control. If you add the runat=server attribute to the tag you can access the input control server side and read the value from it. The postback should fire after the javascript event so everything else should work 'normally' for you.
只有在了解您进行数据绑定的方式之后才能为您的问题提供适当的答案。您是通过调用数据列表的 .DataBind() 方法从代码中进行数据绑定,还是向数据列表提供数据源以进行绑定。我问这个问题的原因是,如果您在绑定数据列表的代码中使用数据列表的 .DataBind() 方法,则必须在每次回发时调用它,以确保数据列表再次获得数据绑定。否则在回发后您将看到数据列表显示为空。如果您使用其 DataSource 或 DataSourceId 属性向数据列表提供要绑定的数据源,则每次回发时数据列表的数据绑定将由框架负责,您不必担心这一点。
但从表面上看,我相信您得到的值为 null,因为您试图在错误的位置获取 DataItem 的值。请记住,您必须遵循控件的正确生命周期,才能使其按照您希望的方式工作。
如果您能回答我上面的问题,我可以提供更好的答案。您当前的陈述似乎不足以提供相关答案。
An appropriate answer to your question can only be provided after knowing the way you are doing the data binding. Are you doing the data binding from the code by calling the .DataBind() method of the datalist or are you providing a datasource to the datalist to bind from. The reason i ask this question is that in case you are using the .DataBind() method of the datalist from the code for binding the datalist, you will have to call it on every postback to make sure that the datalist gets databound again. Otherwise after the post back you will see that the datalist shows up empty. In case you are providing a datasource to the datalist to bind from, using its DataSource or DataSourceId property, the databinding of the datalist on each post back is taken care of by the framework, you don't have to be concerned about that.
But from the look of things i believe that you are getting the value as null because you are trying to get the value of the DataItem at a wrong place. Remember that you will have to follow the proper life cycle of a control to make it work for you the way you want it to work.
I can provide better answer if you can answer my queries above. Your current statement seems insufficient for providing a relevant answer.