简体   繁体   English

从代码隐藏(ASP.NET)进行绑定

[英]Binding from codebehind (ASP.NET)

I have SqlDataSource assigned to a GridView, and a lot of the fields returned are only used in the code behind to determine whether a column is visible, etc. Currently I bind these vlaues to a hiddenfield, however this adds a lot of code to the source file which isn't ideal. 我已将SqlDataSource分配给GridView,并且返回的很多字段仅在后面的代码中用于确定一列是否可见,等等。目前,我将这些vlaues绑定到了hiddenfield,但是这为该字段添加了很多代码源文件不理想。

Can I get the values for the row from the codebehind, without having to assign them to a hiddenfield in the markup? 我可以从后面的代码中获取行的值,而不必将其分配给标记中的hiddenfield吗?

In your code behind, you can access the values through the DataItem object 在后面的代码中,您可以通过DataItem对象访问值

Example

 DataRowView rowView = (DataRowView)e.Row.DataItem; // Retrieve the state value for the current row. String state = rowView["state"].ToString(); 

Or you could convert the value to an object and play around with it. 或者,您可以将值转换为对象并进行操作。

Take a look here for more info http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx 在此处查看更多信息http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.gridviewrow.dataitem.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM