0

我有这个代码示例。该网页将被另一个通过 url 传递 ID 的网页调用,例如 http://localhost:49780/bookingform.aspx?passedid=8

如何将该 ID 获取到我的 selectcommand 以过滤掉我的记录?例如 ProductID <= passid

代码:

<%@ page language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>Repeater.DataSourceID Property Example</title>
</head>

  <body>
    <form id="Form1" runat="server">

      <h3>Repeater.DataSourceID Property Example</h3>

      <asp:repeater id="Repeater1"       
        datasourceid="SqlDataSource1"
        runat="server">

        <headertemplate>
          <table border="1">
            <tr>
              <td><b>Product ID</b></td>
              <td><b>Product Name</b></td>
            </tr>
        </headertemplate>

        <itemtemplate>
          <tr>
            <td> <%# Eval("ProductID") %> </td>
            <td> <%# Eval("ProductName") %> </td>
          </tr>
        </itemtemplate>

        <footertemplate>
          </table>
        </footertemplate>
      </asp:repeater>

            <asp:sqldatasource id="SqlDataSource1"          
            connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" 
        selectcommand="SELECT ProductID, ProductName FROM [Products] Where ProductID <= 10"
        runat="server">
      </asp:sqldatasource>

    </form>      
  </body>
</html>

问候

4

2 回答 2

0
private int _querystring=string.empty;

public int QueryString
{
get
{
return _querystring;
}
set
{
_querystring=value;
}

QueryString=Convert.ToInt32(Request.QueryString["passedid"].ToString());

在您的代码中传递此值

selectcommand='SELECT ProductID, ProductName FROM [Products] Where ProductID="<%=QueryString>"'
于 2012-11-29T11:43:43.143 回答
0

嗨,您可以将查询字符串作为参数传递给 sqlconnection

 <asp:sqldatasource id="SqlDataSource1"          
        connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" 
    selectcommand="Procedure Name" CommandType="Procedure"
    runat="server">
   <SelectParameters>
  <asp:QueryStringParameter Name="ProductID " QueryStringField="passedidc" Type="Int32" />
   <SelectParameter>
  </asp:sqldatasource>

唯一的问题是,您需要使用过程并将过程名称传递给 selectcommand,而不是在页面上使用查询。

于 2012-11-29T11:34:20.553 回答