从 Request.Form 设置 SqlDataSource 参数

发布于 2024-12-03 07:42:38 字数 1576 浏览 0 评论 0原文

我正在尝试创建简单的搜索。我在 MasterPage 中有“txtSearch”文本框和“搜索”按钮,按钮有 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 有什么问题。

I'm trying to create simple search. I have "txtSearch" textBox and "search" button in MasterPage, button has PostbackUrl= Search.aspx

In 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" />

In Search.aspx i have GridView that shows me results and 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>

In Define Parameters :
UserName =Request.Form("txtSearch") as it needs to be, but it's not working. I tried with session["search"]=txtSearch.text; and its working.. But not with Request.Form..

UPD
I tryed to use QueryStringField instead of FormField and it works, don't know whats wrong with FormField.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

徒留西风 2024-12-10 07:42:38

为什么不避免会话变量(例如,我不喜欢“全局”变量高于其他所有变量的想法,并且无法知道它是否被无意中更改或被错误代码损坏),并使用 改为 ControlParameter

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

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

Why not avoid Session variables (I don't like the idea of a 'global' variable that sits above everything else, and no way of knowing if it was inadvertently changed or corrupted by bad code, for example), and use ControlParameter instead?

ie

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

Let ASP.net do most of the work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文