简体   繁体   English

html标记值的字符串表达式

[英]string expression for html tag value

I'm defining a datasource (for a sortable listview) and have a minor snag. 我正在定义一个数据源(用于可排序的列表视图)并且有一个小问题。

Code analogous to this works: 与此类似的代码:

<asp:SqlDataSource ID="myDataSource" runat="server"
  SelectCommand="SELECT [aField], [bField]  FROM Suggestions WHERE stype = 'X'"
  ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" >
</asp:SqlDataSource>

But the real select string is much more complicated, I want to do something like this: 但真正的选择字符串要复杂得多,我想做这样的事情:

<asp:SqlDataSource ID="myDataSource" runat="server"
  SelectCommand="SELECT [aField], [bField]  " +
                     "FROM Suggestions WHERE stype = 'X'"
  ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" >
</asp:SqlDataSource>

I've searched on different variations of the terms: concatentation catenation string tag html asp.net 我搜索了术语的不同变体:concatentation catenation string tag html asp.net

but I can't find anything about it, although it looks like it requires a string literal and not a string expression. 但我找不到任何关于它的东西,虽然看起来它需要字符串文字而不是字符串表达式。 So I'm eventually going to define this entire complicated function as a function in the database, but for now I would like to try something like: 所以我最终将这整个复杂的函数定义为数据库中的一个函数,但是现在我想尝试类似的东西:

<asp:SqlDataSource ID="myDataSource" runat="server"
  SelectCommand="<% qry_str('X'); %>"
  ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" >
</asp:SqlDataSource>

where qry_str() is defined in code behind; 其中qry_str()在代码后面定义; however, that particular code does not work: 但是,该特定代码不起作用:

Code behind is defined as: 代码隐藏定义为:

protected string qry_str(string t)
{
    string s =
    "SELECT [aField], [bField]  " +
             "FROM Suggestions WHERE stype = '" + t + "'";
    return "s";
}

I do not think the code behind is the problem. 我不认为背后的代码是问题所在。 I think it's the asp.net and or the way I'm calling it. 我认为这是asp.net和我称之为的方式。 What is the right way to do what I'm trying to do? 做我正在做的事情的正确方法是什么?

You can set this attribute in some page event handler (whichever fits your workflow better). 您可以在某个页面事件处理程序中设置此属性(更适合您的工作流程)。 For example, in the Page_Load: 例如,在Page_Load中:

protected void Page_Load(object sender, EventArgs e)
{
    string t = "X"; // calculate t here
    myDataSource.SelectCommand = "SELECT [aField], [bField] FROM Suggestions WHERE stype = " + t;
    // other actions
}

You seem to be trying to treat the ASPX as if it's C#. 您似乎试图将ASPX视为C#。 If you added the <% %> markers, you should be able to use the C# concatenation syntax: 如果添加了<%%>标记,则应该能够使用C#连接语法:

<asp:SqlDataSource ID="myDataSource" runat="server" 
                   SelectCommand='<% ="SELECT [aField], [bField] " +
                                      "  FROM suggestions        " +
                                      " WHERE sType='X'          " %>' 
                   ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" > 
</asp:SqlDataSource> 

You might run into trouble using ' quotes inside " quotes inside ' quotes - so you might end up having to go with Andrei's answer. 使用“引号内”引号内的引号可能会遇到麻烦 - 所以你可能最终不得不接受安德烈的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM