本文介绍了在C#写作code一个SqlDataSource后面后面使用一个价值code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用基于参数化输入的SQL数据源.The棘手的事情是,参数化的输入值仅在code是可用的背后,所以我被迫写的SQL数据源来填充数据表在$ C页面背后$ C

I am trying to populate a data table using the SQL datasource based upon parametrized inputs .The tricky thing is that the value of the parametrized input is available only in the code behind so I am forced to write the SQL datasource in the code behind page

我如何去了解它(我使用C#和ASP.Net 4.0)

How do I go about it ( I am using C# and ASP.Net 4.0)

在asp.net页面创建SQL数据源连接目前的code是:

The current code in the asp.net page to create the sql datasource connection is :

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:LicensingConnectionString %>"
        SelectCommand="SELECT * FROM [commissions] where username=@username "></asp:SqlDataSource>

我想在@username使用的值在code。通过这个code检索后面

The value I want to use in @username is retrieved in the code behind by this code

IPrincipal p = HttpContext.Current.User;
        // p.Identity.Name : this is what we will use to call the stored procedure to get the data and populate it
        string username = p.Identity.Name;

谢谢!

推荐答案

您需要处理的数据源的OnSelecting事件,在那里你可以设置参数的值。

You need to handle the OnSelecting event of the data source, and in there you can set the parameter's value.

在你的页面:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:LicensingConnectionString %>"
        SelectCommand="SELECT * FROM [commissions] where username=@username"
        OnSelecting="SqlDataSource1_Selecting" >
    <SelectParameters>
        <asp:Parameter Name="username" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

在您的codebehind:

In your codebehind:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
     e.Command.Parameters["@username"].Value = HttpContext.Current.User.Identity.Name;
}

这篇关于在C#写作code一个SqlDataSource后面后面使用一个价值code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 15:00
查看更多