如何在SelectCommand Asp.net中设置传递变量

时间:2015-01-19 13:59:07

标签: c# asp.net

我尝试这个例子但没有工作。

<%string id = Request.QueryString["id"]; %>// get value and set in one variable
<%=id%>// display successfully variable's value
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
SelectCommand="SELECT [id], [name], [image], [old_price], [new_price] FROM [product] where [id]= "'<%=id%>'"" ></asp:SqlDataSource>

SelectCommand中的问题如何在查询中设置变量我尝试像&lt;%= id%&gt;

1 个答案:

答案 0 :(得分:1)

使用代码隐藏,例如Selecting event

<asp:SqlDataSource ID="SqlDataSource1" ... OnSelecting="Product_Selecting">

代码隐藏:

protected void Product_Selecting(object sender, SqlDataSourceCommandEventArgs e)
{
    e.Command.Parameters[0].Value = Request.QueryString["id"];
}