在 C# .net 后面的代码中从我的 SqlDataSource 访问数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1169703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 09:55:13  来源:igfitidea点击:

Accessing data from my SqlDataSource in the code behind C# .net

c#.netasp.netsqldatasource

提问by

I have a contact page where there is a datalist of people and if you click on one of them you get a contact form that I want to send to that particular person.

我有一个联系页面,里面有一个人的数据列表,如果你点击其中一个人,你会得到一个我想发送给那个人的联系表格。

I use sqldatasource dscontactemail to get information about that person to place on the contact form but how do I get information out of dscontactemail from the code behind for when I'm ready to send that mail?

我使用 sqldatasource dscontactemail 来获取有关该人的信息以放置在联系表单上,但是当我准备好发送该邮件时,如何从背后的代码中从 dscontactemail 中获取信息?

I put a formview on the page to display the person's picture and I can get whatever I want from that dscontactemail with <%#Eval("email") for example, but how do I get that from the code behind?

我在页面上放了一个表单视图来显示这个人的图片,我可以从那个 dscontactemail 中得到我想要的任何东西,例如 <%#Eval("email") ,但是我如何从后面的代码中得到它?

I tried a hidden field but it didn't work.

我尝试了一个隐藏字段,但没有用。

Any other ways to access the SqlDataSource on a page from the code behind?

从后面的代码访问页面上的 SqlDataSource 的任何其他方法?

回答by Spencer Ruport

First, you really should graduate from using SqlDataSource to the classes available in the SqlClient namespace at some point so keep that in mind.

首先,您确实应该在某个时候从使用 SqlDataSource 过渡到 SqlClient 命名空间中可用的类,因此请记住这一点。

Here's the code to do it though:

不过,这是执行此操作的代码:

DataView dview = CType(yourSqlDataSource.Select(DataSourceSelectArguments.Empty), DataView);
string str = "";

foreach(DataRow drow dview.Table.Rows)
{
    str += drow("yourcol1").ToString() + "<br />";
}

回答by Fandango68

The following is more than sufficient:

以下内容就足够了:

dv = yourSqlDataSource.Select(DataSourceSelectArguments.Empty) as DataView;

And interrogate the DataView separately.

并分别查询DataView。

If intellisense does not pickup yourSqlDataSource, then simply bind it via the FindControl() of the control that has the data source:

如果智能感知没有选择 yourSqlDataSource,那么只需通过具有数据源的控件的 FindControl() 绑定它:

        SqlDataSource sdsPreStartDates = (SqlDataSource)DetailsView1.FindControl("sdsPreStartDates");
        if (sdsPreStartDates != null)
        {
            DataView dv1 = (DataView)sdsPreStartDates.Select(DataSourceSelectArguments.Empty);
..
.. etc
}