SqlDataSource动态设置SelectCommand

发布于 2024-07-28 18:18:21 字数 561 浏览 3 评论 0原文

我正在使用 SqlDataSource,为了避免直接在代码中编写长查询,我认为我可以创建一个 Query 类,它以字符串形式返回我想要的查询。 我尝试了下面的代码,但我只是得到“服务器标签不能包含 <% ... %> 结构。”

在我使用存储过程之前,但我的虚拟主机不允许这样做,所以这就是我考虑查询类解决方案的时候。 我还需要补充一点,我不想在代码隐藏中进行数据绑定。

有办法做到这一点吗?

    <asp:SqlDataSource ID="DS" 
        runat="server"
        DataSourceMode="DataSet"  
        ConnectionString="<%$ ConnectionStrings:conn %>"
        ProviderName="MySql.Data.MySqlClient"
        SelectCommand="<% Query.getTestQuery() %>"
        >
    </asp:SqlDataSource>

I'm using a SqlDataSource and to avoid writing long queries directly in my code I thought I could make a Query class that returns the query I want as a string. I tried the code below but I just get "Server tags cannot contain <% ... %> constructs."

Before I was using stored procedures but my webhosting doesn't allow that, so thats when I thought about the Query class solution. I also need to add that I don't want to do databinding in codebehind.

Is there a way of doing this?

    <asp:SqlDataSource ID="DS" 
        runat="server"
        DataSourceMode="DataSet"  
        ConnectionString="<%$ ConnectionStrings:conn %>"
        ProviderName="MySql.Data.MySqlClient"
        SelectCommand="<% Query.getTestQuery() %>"
        >
    </asp:SqlDataSource>

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

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

发布评论

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

评论(2

殊姿 2024-08-04 18:18:44

也许使用 ObjectDataSource。 这样,在您的对象内部,您将能够在那里进行动态查询。

否则,您可以在代码隐藏中的 SqlDataSource 选择事件期间修改 SelectCommand。 然后你可以将 SelectCommand 更改为你喜欢的任何内容。

Perhaps use an ObjectDataSource. That way, inside your object, you'll be able to do your dynamic query there.

Otherwise, you could modify the SelectCommand during the SqlDataSource Selecting Event in your code-behind. Then you could change the SelectCommand to whatever you like.

凉风有信 2024-08-04 18:18:38

在 ASP.NET 中无法执行此操作。 <% %> 标签中的代码用于运行任意代码,而不是用于设置属性值。 <%= %> 标签中的代码用于渲染到输出流。 <%# %> 中的代码用于数据绑定,这与您想要完成的任务类似,但并不相同。 <%$ %> 中的代码用于表达式绑定,但它们在页面生命周期中执行得太早,因此不太可能在您的场景中工作。

使用 ObjectDataSource 是一种方法,因为此时它只是代码,并且您的代码可以根据页面的状态执行任何操作。

使用派生的 SqlDataSource 是另一种选择。 创建一个从 SqlDataSource 派生的控件并重写其 OnInit 方法。 在 OnInit 方法中,您可以动态设置 SelectCommand 以根据页面状态执行您想要的操作。

It is not possible to do this in ASP.NET. Code in <% %> tags is for running arbitrary code, not for setting property values. Code in <%= %> tags is for rendering to the output stream. Code in <%# %> is for databinding, which is similar to what you're trying to accomplish, but it's not the same. Code in <%$ %> is for expression bindings, but they execute too early in the page's life cycle and are thus unlikely to work in your scenario.

Using ObjectDataSource is one way to go here since at that point it's just code, and your code can do whatever it wants based on the state of the page.

Using a derived SqlDataSource is another option. Create a control that derives from SqlDataSource and override its OnInit method. In the OnInit method you can dynamically set the SelectCommand to do whatever you want based on the state of the page.

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