C# html标记值的字符串表达式
我正在定义一个数据源(用于可排序列表视图),但遇到了一个小问题 与本工程类似的代码:C# html标记值的字符串表达式,c#,asp.net,sqldatasource,string-concatenation,C#,Asp.net,Sqldatasource,String Concatenation,我正在定义一个数据源(用于可排序列表视图),但遇到了一个小问题 与本工程类似的代码: <asp:SqlDataSource ID="myDataSource" runat="server" SelectCommand="SELECT [aField], [bField] FROM Suggestions WHERE stype = 'X'" ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" &
<asp:SqlDataSource ID="myDataSource" runat="server"
SelectCommand="SELECT [aField], [bField] FROM Suggestions WHERE stype = 'X'"
ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" >
</asp:SqlDataSource>
我不认为背后的代码是问题所在。我认为这是asp.net,或者我称之为asp.net的方式。做我想做的事情的正确方法是什么?您可以在某个页面事件处理程序中设置此属性(以更适合您的工作流的为准)。例如,在加载页面中:
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
}
你似乎试图把ASPX当作C#来对待。如果添加了标记,则应该能够使用C#连接语法:
<asp:SqlDataSource ID="myDataSource" runat="server"
SelectCommand='<% ="SELECT [aField], [bField] " +
" FROM suggestions " +
" WHERE sType='X' " %>'
ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" >
</asp:SqlDataSource>
您可能会在使用“引号内的引号”时遇到问题,因此您可能最终不得不使用Andrei的答案。就是这样!谢谢!将在2分钟内将此标记为正确答案。
protected string qry_str(string t)
{
string s =
"SELECT [aField], [bField] " +
"FROM Suggestions WHERE stype = '" + t + "'";
return "s";
}
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
}
<asp:SqlDataSource ID="myDataSource" runat="server"
SelectCommand='<% ="SELECT [aField], [bField] " +
" FROM suggestions " +
" WHERE sType='X' " %>'
ConnectionString="<%$ ConnectionStrings:dbConnectionString %>" >
</asp:SqlDataSource>