sqldatasourse中的查询字符串值

时间:2012-02-10 10:50:19

标签: c# asp.net-3.5 webforms sqldatasource

我有以下工作正常:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="test_library_newsletter_Default2" %>

<!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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">

    <div  style="font-family:Arial;">
        <asp:gridview id="CustomersGridView" 
        datasourceid="CustomersSource" 
        autogeneratecolumns="false"
        emptydatatext="No data available." 
        allowpaging="true" 
        runat="server"
        ShowHeader="True"
        GridLines="None"
        CellPadding="10">

        <Columns>
            <asp:BoundField HeaderText="Date"
            DataField="date"
            dataformatstring="{0:dd-MM-yyyy}" />

            <asp:HyperLinkField HeaderText="Subject"
            DataTextField="subject"
            DataNavigateUrlFields="id"
            DataNavigateUrlFormatString="http://intranet/Default2.aspx?id={0}" />
        </Columns>

        <pagersettings mode="Numeric"
        position="Bottom"           
        pagebuttoncount="10"/>

            <HeaderStyle HorizontalAlign="Left" />


        </asp:gridview>

        <asp:sqldatasource id="CustomersSource"
        selectcommand="select id, subject, date from table order by id desc"
        connectionstring="connection string here" 
        runat="server"/>

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

但是,我想在sql语句中使用查询字符串值。我该怎么做?

1 个答案:

答案 0 :(得分:2)

你需要这样的东西

<asp:SqlDataSource ID="ID" runat="server" ConnectionString="connectionString"
    ProviderName="providerName"
    SelectCommand="SELECT [ProductID], [ProductName], [UnitPrice] FROM [Products] WHERE     ([CategoryID] = ?)">
    <SelectParameters>
        <asp:QueryStringParameter Type="Int32" 
            Name="CategoryID" 
            QueryStringField="CategoryID" />
    </SelectParameters>
</asp:SqlDataSource>

阅读完整帖子 http://www.4guysfromrolla.com/articles/030106-1.aspx

希望它有所帮助。