For SqlDataSource I can configure the external source for the incoming paramater. For example it might be a QueryString, Session, Profile and so on. However I do not have an option to use User as a source.
对于SqlDataSource,我可以为传入参数配置外部源。例如,它可能是QueryString,Session,Profile等。但是,我没有选择使用User作为源。
I know that I could provide value for the parameter in Inserting,Selecting,Updating,Deleting events. But I do not think that this is an ellegant solution because I have some parameteres already definied in aspx file. I do not want to have parameters defined in two separate places. It makes mess.
我知道我可以在插入,选择,更新,删除事件中为参数提供值。但我认为这不是一个优雅的解决方案,因为我已经在aspx文件中定义了一些参数。我不想在两个不同的地方定义参数。它变得一团糟。
So can I somehow define this parameter in .aspx file?
那么我可以在.aspx文件中以某种方式定义此参数吗?
<SelectParameters>
<asp:QueryStringParameter DefaultValue="-1" Name="ID"
QueryStringField="ID" />
//User.Identity.Name goes here as a value for another parameter
</SelectParameters>
3 个解决方案
#1
13
Declare it in your .aspx
and fill it in your codebehind:
在你的.aspx中声明它并将其填入你的代码隐藏:
aspx
ASPX
<asp:Parameter Name="username" Type="String" DefaultValue="Anonymous" />
codebehind
代码隐藏
protected void Page_Init(object sender, EventArgs e) {
DataSource.SelectParameters["username"].DefaultValue = User.Identity.Name;
}
#2
1
in asp page put a blank datasource with a connectionstring
在asp页面中放入一个带有连接字符串的空白数据源
e.g.
例如
<asp:SqlDataSource ID="SqlDataSourceDeviceID" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>">
<asp:DropDownList ID="DropDownListDeviceID" runat="server" DataSourceID="SqlDataSourceDeviceID" DataTextField="DevLoc" DataValueField="DeviceId"></asp:DropDownList>
in code behind on pageload
在pageload背后的代码中
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
String myQuery =String.Format("SELECT DeviceID,DevLoc FROM ... where UserName='{0}')",User.Identity.Name);
SqlDataSourceDeviceID.SelectCommand = myQuery;
SqlDataSourceDeviceID.DataBind();
DropDownListDeviceID.DataBind();
}
#3
1
You can also accomplish this by creating a hidden textbox on the page, apply the User.Identity.Name to the value and then use the formcontrol parameter in the SQL data source. The advantage here is that you can reuse the code in the select, insert, delete, and update parameters without extra code.
您还可以通过在页面上创建隐藏文本框,将User.Identity.Name应用于值,然后在SQL数据源中使用formcontrol参数来完成此操作。这里的优点是您可以在select,insert,delete和update参数中重用代码而无需额外的代码。
So in aspx we have (noneDisplay is a css class to hide it):
所以在aspx中我们有(noneDisplay是一个隐藏它的css类):
<asp:TextBox runat="server" ID="txtWebAuthUser" CssClass="noneDisplay"></asp:TextBox>
and in the Update parameter of the sql datasource update section:
并在sql数据源更新部分的Update参数中:
<asp:ControlParameter Name="CUrrentUser" ControlID="txtWebAuthUser" Type="String" PropertyName="Text" />
which gets interpreted in the update something like this:
在更新中解释如下:
UpdateCommand="UPDATE [Checks] SET [ScholarshipName] = @ScholarshipName, [Amount] = @Amount,LastModifiedBy=@CUrrentUser,
[LastModified] = getdate() WHERE [CheckId] = @CheckId"
and then in the .cs file form load we have:
然后在.cs文件表单加载中我们有:
this.txtWebAuthUser.Text = User.Identity.Name;
This technique has worked well in many places in all of our applications.
这种技术在我们所有应用程序的许多地方都运行良好。
#1
13
Declare it in your .aspx
and fill it in your codebehind:
在你的.aspx中声明它并将其填入你的代码隐藏:
aspx
ASPX
<asp:Parameter Name="username" Type="String" DefaultValue="Anonymous" />
codebehind
代码隐藏
protected void Page_Init(object sender, EventArgs e) {
DataSource.SelectParameters["username"].DefaultValue = User.Identity.Name;
}
#2
1
in asp page put a blank datasource with a connectionstring
在asp页面中放入一个带有连接字符串的空白数据源
e.g.
例如
<asp:SqlDataSource ID="SqlDataSourceDeviceID" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>">
<asp:DropDownList ID="DropDownListDeviceID" runat="server" DataSourceID="SqlDataSourceDeviceID" DataTextField="DevLoc" DataValueField="DeviceId"></asp:DropDownList>
in code behind on pageload
在pageload背后的代码中
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
String myQuery =String.Format("SELECT DeviceID,DevLoc FROM ... where UserName='{0}')",User.Identity.Name);
SqlDataSourceDeviceID.SelectCommand = myQuery;
SqlDataSourceDeviceID.DataBind();
DropDownListDeviceID.DataBind();
}
#3
1
You can also accomplish this by creating a hidden textbox on the page, apply the User.Identity.Name to the value and then use the formcontrol parameter in the SQL data source. The advantage here is that you can reuse the code in the select, insert, delete, and update parameters without extra code.
您还可以通过在页面上创建隐藏文本框,将User.Identity.Name应用于值,然后在SQL数据源中使用formcontrol参数来完成此操作。这里的优点是您可以在select,insert,delete和update参数中重用代码而无需额外的代码。
So in aspx we have (noneDisplay is a css class to hide it):
所以在aspx中我们有(noneDisplay是一个隐藏它的css类):
<asp:TextBox runat="server" ID="txtWebAuthUser" CssClass="noneDisplay"></asp:TextBox>
and in the Update parameter of the sql datasource update section:
并在sql数据源更新部分的Update参数中:
<asp:ControlParameter Name="CUrrentUser" ControlID="txtWebAuthUser" Type="String" PropertyName="Text" />
which gets interpreted in the update something like this:
在更新中解释如下:
UpdateCommand="UPDATE [Checks] SET [ScholarshipName] = @ScholarshipName, [Amount] = @Amount,LastModifiedBy=@CUrrentUser,
[LastModified] = getdate() WHERE [CheckId] = @CheckId"
and then in the .cs file form load we have:
然后在.cs文件表单加载中我们有:
this.txtWebAuthUser.Text = User.Identity.Name;
This technique has worked well in many places in all of our applications.
这种技术在我们所有应用程序的许多地方都运行良好。