我有一个sqlDataSource,我在其中使用select参数,如下所示:
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"
ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>" RunAt="server">
<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="<%#userid%>" />
</SelectParameters>
</asp:sqlDataSource>
在我的后端代码中,我为userId分配了一个值,并且还写了this.DataBind();
数据绑定不起作用。有谁知道解决这个问题?
答案 0 :(得分:2)
一种方法是在Page_Load事件后面的代码中或在绑定变量后使用userid变量设置值。
//set SqlDataSource parameters value with empID as your variable
EmployeeDetailsSqlDataSource.SelectParameters.Add("@EmpID", userID);
天冬氨酸:
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:sqlDataSource>
另一种选择是在数据检索之前的选择事件中设置代码中的值。
<强> SqlDataSource.Selecting Event 强>
以下是指向更完整示例的链接 Full Example
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"
RunAt="server" OnSelecting=OnSelectingHandler">
<SelectParameters>
<asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:sqlDataSource>
代码背后:
Public void OnSelectingHandler(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters[0].Value=userID;
}