有一个gridview,这是数据源
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString2 %>"
SelectCommand="SELECT [loginname], [title], [username], [rdate] FROM [View_1] ORDER BY [tdate] DESC">
</asp:SqlDataSource>还有一个dropdownlist是绑定数据库视图的username的。怎样获取dropdownlist里当前选中的值,写在gridview数据源的SQL语句里呢?就是。后面加个where条件。按姓名查找。我前面问题没写清楚。对不起大家。
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:test1ConnectionString2 %>"
SelectCommand="SELECT [loginname], [title], [username], [rdate] FROM [View_1] ORDER BY [tdate] DESC">
</asp:SqlDataSource>还有一个dropdownlist是绑定数据库视图的username的。怎样获取dropdownlist里当前选中的值,写在gridview数据源的SQL语句里呢?就是。后面加个where条件。按姓名查找。我前面问题没写清楚。对不起大家。
{
//DropDownList1的AutoPostBack设置为true
string sql = "select * from table where xxx='" + this.DropDownList1.SelectedValue + "'";
Gridview1.DataSource = ReturnDataTable(sql);
Gridview1.DataBind();
}
public static DataTable ReturnDataTable(string cmdtext)
{
SqlConnection cn = new SqlConnection();
cn.ConnectionString = "数据库连接字符串";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand();
cmd = new SqlCommand(cmdtext, cn);
cmd.CommandType = CommandType.Text; ;
SqlDataReader dr = null;
using (dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(dr);
}
return dt;
}
dt = SqlHelper.ExecuteDataTable(SqlHelper.ConnectionStringLocalTransaction, CommandType.Text, "select id,name from tablename", null);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "name";//绑定的文本内容,也就是下拉列表中的内容
DropDownList1.DataValueField = "id";//对应的value值
DropDownList1.DataBind();
DropDownList1.Items.Insert(0,new ListItem("请选择","0"));