4

我的 page2.aspx.cs 页面中有一个属性

public int? Id
{
        get
        {
            if (ViewState[Page1.Id] != null)
                return Convert.ToInt32(ViewState[Page1.Id]);
            else return null;
        }
        set
        {
            ViewState[Id] = value;
        }
    }

我正在使用 asp: 数据源:

<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
   ProviderName="System.Data.SqlClient" 
   SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id = need to pass property value >" 
   OnSelecting="SqlDataSourceGridView_Selecting">
</asp:SqlDataSource>

我想在我的 where 条件下传递 Id。这怎么可能。

请有人帮助我。

4

4 回答 4

4

来看看

 <asp:sqlDataSource ID="EmployeeDetailsSqlDataSource" 
   SelectCommand="SELECT EmployeeID, LastName, FirstName FROM Employees WHERE EmployeeID = @EmpID"

   InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName); 
                  SELECT @EmpID = SCOPE_IDENTITY()"
   UpdateCommand="UPDATE Employees SET LastName=@LastName, FirstName=@FirstName 
                    WHERE EmployeeID=@EmployeeID"
   DeleteCommand="DELETE Employees WHERE EmployeeID=@EmployeeID"

   ConnectionString="<%$ ConnectionStrings:NorthwindConnection %>"
   OnInserted="EmployeeDetailsSqlDataSource_OnInserted"
   RunAt="server">

   <SelectParameters>
     <asp:Parameter Name="EmpID" Type="Int32" DefaultValue="0" />
   </SelectParameters>

   <InsertParameters>
     <asp:Parameter Name="EmpID" Direction="Output" Type="Int32" DefaultValue="0" />
   </InsertParameters>

 </asp:sqlDataSource>
于 2012-04-27T08:39:21.870 回答
1

1)插入带有参数名称的占位符,如@Id您选择查询

2)将参数的参数规范添加Id<SelectParameter>SqlDataSource 部分

<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
     ProviderName="System.Data.SqlClient" 
     SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id = @Id" 
     OnSelecting="SqlDataSourceGridView_Selecting">

    <SelectParameters>
         <asp:Parameter Name="Id" Type="Int32" />
    </SelectParameters>

</asp:SqlDataSource>

3)在选择事件方法中将参数值放入命令

protected void SqlDataSourceGridView_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["@Id"].Value = (int)(Id ?? 0); // nullable ind is not applicable here
}
于 2012-04-27T08:51:46.580 回答
0

您可以从后面的代码中设置它。只需在您的 sql 数据源中定义一个选择参数,例如

    <asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
   ProviderName="System.Data.SqlClient" 
   SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id =@id" 
   OnSelecting="SqlDataSourceGridView_Selecting">
  <SelectParameters>
        <asp:Parameter Name="@id" Type="Int32" />
    </SelectParameters>

然后在你后面的代码中你可以手动设置参数,比如

SqlDataSourceGridView.SelectParameters["@id"].DefaultValue = yourValue;
Gridview1.DataBind();
于 2012-04-27T09:25:06.230 回答
0

您必须在 SQLDataSource 下使用 Node。

看看这里:MSDN 参考教程

<asp:SqlDataSource ID="SqlDataSourceGridView" runat="server" 
   ProviderName="System.Data.SqlClient" 
   SelectCommand="SELECT [Name],[Address] FROM [Table_Emp] where Id = need to pass property value >" 
   OnSelecting="SqlDataSourceGridView_Selecting">

   <SelectParameters>
     <asp:Parameter Name="ParameterName" />
   </SelectParameters>

</asp:SqlDataSource>

OT:您的属性 ID 有错误。您设置了 ViewState[Id] 但您尝试检索 Viewstate[Page.id]

于 2012-04-27T08:47:16.497 回答