从Request.Form设置SqlDataSource参数

时间:2011-09-04 23:01:32

标签: asp.net sqldatasource

我正在尝试创建简单的搜索。我在MasterPage中有“txtSearch”textBox和“search”按钮,按钮有PostbackUrl = Search.aspx

在MasterPage中

<asp:TextBox ID="txtSearch" runat="server" ValidationGroup="b" Text="Users Search" ForeColor="Silver">Users Search</asp:TextBox>

<asp:Button ID="btnSearch" runat="server" Text="Search" ValidationGroup="b" 
PostBackUrl="~/Search.aspx" onclick="btnSearch_Click" />

在Search.aspx中,我有GridView显示结果和SqlDataSource

<asp:SqlDataSource ID="SqlDataSearchResult" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

    SelectCommand="SELECT vw_aspnet_Users.UserId, vw_aspnet_Users.UserName, Custon_UserInfo.UserId AS Expr1, Custon_UserInfo.FirstName, Custon_UserInfo.LastName, Custon_UserInfo.Location, Custon_UserInfo.Avatar, Custon_MoneyWork.UserId AS Expr2, Custon_MoneyWork.Money, vw_aspnet_Users.LastActivityDate FROM vw_aspnet_Users INNER JOIN Custon_UserInfo ON vw_aspnet_Users.UserId = Custon_UserInfo.UserId INNER JOIN Custon_MoneyWork ON vw_aspnet_Users.UserId = Custon_MoneyWork.UserId WHERE (vw_aspnet_Users.UserName LIKE '%' + @UserName + '%')">
    <SelectParameters>
        <asp:FormParameter FormField="txtSearch" Name="UserName" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>

在定义参数中: UserName = Request.Form(“txtSearch”),因为它需要,但它不起作用。我试过session [“search”] = txtSearch.text;和它的工作..但不是与Request.Form ..

UPD 我尝试使用QueryStringField而不是FormField,它可以工作,不知道FormField有什么问题。

1 个答案:

答案 0 :(得分:0)

为什么不避免使用Session变量(我不喜欢一个位于其他所有变量之上的全局变量的概念,并且无法知道它是否被不良代码无意中更改或损坏,例如),并使用ControlParameter代替?

<SelectParameters>
        <asp:ControlParameter ControlID="txtSearch" Name="UserName" PropertyName="Text" Type="String" />
    </SelectParameters>

让ASP.net为您完成大部分工作。