在代码隐藏(ASPX 页面)中构建一个表(包含行和列)
在 ASPX 页面的代码隐藏中,我获得了一些有关员工的详细信息(从外部数据源读取)。最后我想以下面的方式展示。下面的显示可能不正确,但它是一个带有标题/列方法的简单表格。
___________________________________________________________________________________
| DEPT | HR | |__________________________________________________________________________________|
| Employee Image | Emp Name | Hire Date |
|____________________________________|_____________________________________________|
| Steve.gif | Steve Jobs | 22/05/1979 |
|____________________________________|_____________________________________________|
| Mark.gif | Mark Miller | 22/05/1949 |
|____________________________________|_____________________________________________|
| DEPT | Operations | |__________________________________________________________________________________|
| Employee Image | Emp Name | Hire Date |
|____________________________________|_____________________________________________|
数据是从各种数据源收集的,并最终在代码隐藏中可用。我想以上述方式显示。
最好的方法是什么?我想创建一个 htmldivcontrol 并添加所有这些值。最后我将 div 标签绑定到页面。
目前我正在尝试方法,但想知道是否有更好的方法。
请分享您的观点和例子。 _
Within codebehind in an ASPX page I get few details about the employees (read from external data source). Finally I would like to show in the below fashion. The display of below may be showin incorrect but is a simplpe table with header/column approach.
___________________________________________________________________________________
| DEPT | HR | |__________________________________________________________________________________|
| Employee Image | Emp Name | Hire Date |
|____________________________________|_____________________________________________|
| Steve.gif | Steve Jobs | 22/05/1979 |
|____________________________________|_____________________________________________|
| Mark.gif | Mark Miller | 22/05/1949 |
|____________________________________|_____________________________________________|
| DEPT | Operations | |__________________________________________________________________________________|
| Employee Image | Emp Name | Hire Date |
|____________________________________|_____________________________________________|
The data is collected from various data sources and is finally available within the codebehind. I want to display in the above fashion.
What is the best approach? I thought of creating a htmldivcontrol and adding all these values. Finally I will bind div tag to the page.
Currently I am trying with approach but want to know if there are any better approaches.
Please share your views and exaamples.
_
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在我看来,最好的方法是使用这样的中继器:
然后在后面的代码中,类似:
您需要将网格绑定到
List
其中Employee
的定义如下:但是他们必须在绑定之前按部门排序。
填充转发器的示例代码
生成
The best approach in my opinion, would be to use a Repeater like this:
And then on your code behind, something like:
You would need to bind your grid to a
List<Employee>
whereEmployee
would be defined like so:But they must be ordered by Department previously to being bound.
Sample code to Populate the repeater
Produces
GridView 可能是最好的
A GridView would probably be best
您是否正在考虑动态地制作表格?以后你编辑它会很困难。为什么不在标记(.aspx)中制作一个普通的 HtmlTable,放置一些标签和图像,然后将其填充到代码隐藏中?有什么特殊原因吗?
Are you thinking of doing the table dynamically? You'll have a hard-time editing it in the future. Why not make an ordinary HtmlTable in your markup(.aspx), put some Labels and Images, then populate it in code-behind? Are there any special reasons why?
你有几个选择。
一种方法是将所有不同的数据合并到一个动态创建的数据表中并绑定到数据源。
另一种方法是简单地执行您的建议并发出包含相关信息的 div。
第三种方法是在 aspx 文件中定义表格并使用 asp:label 控件。在后面的代码中,填充来自各种数据源的标签控件。
考虑到这些选择,我可能会选择第三个。听起来最容易在需要时重新配置/更改显示。
You have a couple of options.
One way would be to combine all of the disparate data into a single datatable that you create on the fly and bind to your data source.
Another is to simply do what you suggested and emit div's with the relevant info.
A third is to define the table in your aspx file and use asp:label controls. In your code behind populate the label controls from your various data sources.
Given those options I'd probably go with the third one. Sounds easiest to reconfigure / change the display of when you need to.