如何以编程方式增加值在ASP.NET DataList控件/数据源? [英] How to add values programmatically to a datalist/datasource in ASP.NET?

查看:102
本文介绍了如何以编程方式增加值在ASP.NET DataList控件/数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加值不是从在SqlDataSource未来即在DataList被连接到一个DataList的方法。这些值不一定在我的数据库,但仍然需要在DataList控件我已经有露面找到。

I am in need of a method of adding values to a DataList that are not coming from the SQLDataSource that the DataList is connected to. These values are not necessarily found in my database but nonetheless need to show up in the DataList I already have.

在我的网页我有这样的结构:

On my page I have this structure:

DataListA
    Hidden Field
    Label
    DataListB
    SQLDataSourceB

SQLDataSourceA

我要寻找方法以编程方式(即在自定义的)添加条目SQLDataSourceB这样DataListB包括我DataListB需要显示这些额外行。我看着到pre_Render事件和例子,如这里找到 ,但我并不需要更改的条目,也不改变自己的风格。我要寻找的添加自定义条目,有没有办法做到这一点?

I am looking for methods to programmatically add entries (custom ones at that) to SQLDataSourceB such that DataListB includes these extra rows I need displaying in DataListB. I've looked into the Pre_Render events and examples such as found here but I don't need to change entries, nor alter their style. I am looking for adding custom entries, is there a way to do this?

编辑:的DataListB正在显示是一个部门,code和名的结果。这些显示的学校课程。例如,一个典型的结果将是这样的: ART 3000摄影
。我要找添加到DataListB课程是在相同的格式上面的例子定制课程。这背后的原因 - 如果我展示 ART 3000摄影在DataListB,我想显示 SCI 1000生物,而不是如我并没有显示 ART 3000摄影,我希望 SCI 1100化学。这是案件众多,如这一点,我需要能够检查我什么显示课程(页面加载之前)并改变我需要添加到DataListB其自定义类。

The results that DataListB is displaying is an "Department", "Code" and "Name". These are displaying school courses. Ie, a typical result would look like: ART 3000 Photography . Courses that I am looking to add to DataListB are custom courses that are in the same format as the above example. The reason behind this - if I display ART 3000 Photography in DataListB, I want to display SCI 1000 Biology rather than if I didn't display ART 3000 Photography , I would want SCI 1100 Chemistry. It is a multitude of cases such as this that I need to be able to check what courses I am displaying (before page load) and alter which custom classes I need to add to DataListB.

@ ethorn10 我有添加这些定制的课程,我的DataList的两个主要需求。第一种情况是对一系列类。对于这个范围内显示表中的每一类改变类的x个更多的,我从这个池需要。例如,让我们说我有一系列的类以code从1000到3000的类此范围应在表中(至少)显示的是6这样的限度内,(我实现某种,而不是在数据列表中的条目的标签是必要的这一)我希望用户看到1000 6之间更多的类,以3000。对于在此范围内的每个过程中所显示的,我要的6递减,因此,如果一个类是采取在范围内,显示/标签应改为1000之间的5个班3000。

@ethorn10 I have two primary needs of adding these custom courses to my DataList. The first case is for a range of classes. For every class displayed in the table within this range alters the x number of classes more that I need from this pool. For example let's say I have a range of classes with a "Code" from 1000 to 3000. The limit of classes within this range that should (at minimum) display in the table is 6. Thus, (and I realize a label of some sort, rather than an entry in the datalist is necessary for this) I want the user to see "6 more classes between 1000 to 3000". For every course in this range that is displayed, I want the "6" to decrement, thus if one class is taken in the range, the display/label should read "5 more classes between 1000 to 3000".

第二种情况是类似于第一,但不是被包容从1000至3000,还有是不同的类的一个池。因此,它更改为你需要2个班从这些类:1200,1350,2300,等等......

The second case is similar to the first, but instead of being inclusive from 1000 to 3000, there are is a pool of distinct classes. Thus it changes to "You need 2 more classes from these classes: 1200, 1350, 2300, etc..."

推荐答案

您可以使用数据视图来获得的SqlDataSource 数据,然后转换为数据视图数据表。之后,你可以添加自定义的数据作为数据表

You can use DataView to get the SqlDataSource data and then convert to DataView to Datatable. After that you can add custom data as Rows to the DataTable.

DataView dv =(DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
DataTable dt = dv.ToTable();
dt.Rows.Add(1, "item1", "item2", 3, "item4"); //the number of param should match the number of column you query returns
GridView1.DataSource = dt; //binding the datatable to a gridview after adding customer record
GridView1.DataBind();

这篇关于如何以编程方式增加值在ASP.NET DataList控件/数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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