我的代码中有以下两个控件
gridview(用于数据显示) sqldatasource(我的gridview的源)
我想更改gridview,但是当我更改我的sqldatasource 中的sqlcommandstring 时,在我的datagrid 表排序的新视图中,第一次单击不会触发。请参阅下面的 sqlcommandstring 更改过程。
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (IsPostBack)
{ // Restore saved sqlcommand in page refresh, please see below
SqlDataSource1.SelectCommand = ViewState["MySQL"].ToString();
}
}
catch (Exception ex){}
}
protected void btn_SearchLibrary_Click(object sender, EventArgs e)
{
SqlDataSource1.SelectCommand = "SELECT * FROM Books WHERE id=1 OR id=2";
ViewState["MySQL"] = "SELECT * FROM Books WHERE id=1 OR id=2";
//Saves sqlcommand in viewstate, to restore it in page refresh.
}