我在客户端设置了asp:GridView
,如下所示:
<asp:GridView ID="GridView1" DataSourceID="SqlDataSource1" AutoGenerateColumns="true"
AutoGenerateDeleteButton="true" AutoGenerateEditButton="true" CssClass="GV" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt" AllowPaging="true"
runat="server">
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:MyConnection%>" runat="server">
</asp:SqlDataSource>
然后在后面的代码中,我在try / catch语句中设置了DataKeyNames
和asp:SQLDataSource
个参数。即使我在第一次尝试时发表评论,我也无法解雇第二个。
protected void Page_Load(object sender, EventArgs e)
{
string qs = Request.QueryString["param"];
string id = Request.QueryString["id"];
if (qs != null)
{
try
{
if (qs == "Department")
{
GridView1.DataKeyNames = new string[] {"id"};
SqlDataSource1.SelectCommand = "SELECT * FROM [table2] "
+ "WHERE Department_Name LIKE'" + id + "' ORDER BY [Department_Name] DESC";
SqlDataSource1.UpdateCommand = "UPDATE table2 SET Department_Name=@Department_Name, Phone=@Phone, "
+ "Fax=@Fax, Contact=@Contact, Address=@Address, City=@City, State=@State "
+ "WHERE (id = @id)";
SqlDataSource1.DeleteCommand = "DELETE FROM table2 WHERE id = @id";
}
}
catch (Exception ex)
{
SqlDataSource1.SelectCommand = "SELECT * FROM [table1]";
//ApplicantsSqlDataSource.UpdateCommand = "";
//ApplicantsSqlDataSource.DeleteCommand = "";
GridView1.Visible = false;
NoResults.Text = "<p>Sorry, there are no results that match your search query.<br />" + ex + "</p>";
}
}
}
这是点击事件
protected void SearchDept_Click(object sender, EventArgs e)
{
TextBox txtSearchDept = (TextBox)Page.FindControl("txtSearchDept");
if (txtSearchDept.Text.Length > 0)
{
Response.Redirect("Default.aspx?param=Department&id=" + txtSearchDept.Text.ToString());
}
else
{
NoResults.Text = "<p>Please enter a search parameter.</p>";
}
}
它应该可以工作,但它没有
被修改 这是第一个尝试捕获的工作,最初被遗漏
try
{
if (qs == "LastName")
{
GridView1.DataKeyNames = new string[] {"EMPLOYEE"};
SqlDataSource1.SelectCommand = "SELECT * FROM [table1] "
+ "WHERE Last_Name='" + id + "' ORDER BY [EMPLOYEE] DESC";
SqlDataSource1.UpdateCommand = "UPDATE table1 SET FIRST_NAME=@FIRST_NAME, LAST_NAME=@LAST_NAME, "
+ "TITLE=@TITLE, DATE_HIRED=@DATE_HIRED, WK_PHONE_NBR=@WK_PHONE_NBR, WK_PHONE_EXT=@WK_PHONE_EXT, "
+ "EMAIL_ADDRESS=@EMAIL_ADDRESS, DEPARTMENT=@DEPARTMENT, PROCESS_LEVEL=@PROCESS_LEVEL, CELL_PHONES=@CELL_PHONES, FAX_NUM=@FAX_NUM "
+ "WHERE (EMPLOYEE = @EMPLOYEE)";
SqlDataSource1.DeleteCommand = "DELETE FROM table1 WHERE EMPLOYEE = @EMPLOYEE";
}
}
catch (Exception ex)
{
SqlDataSource1.SelectCommand = "SELECT * FROM [table1]";
//ApplicantsSqlDataSource.UpdateCommand = "";
//ApplicantsSqlDataSource.DeleteCommand = "";
GridView1.Visible = false;
NoResults.Text = "<p>Sorry, there are no results that match your search query.<br />" + ex + "</p>";
}
答案 0 :(得分:0)
您需要在部门区域的SelectCommand
中添加百分号。
按照正确的格式:SqlDataSource1.SelectCommand = "SELECT * FROM [table2] " + "WHERE Department_Name LIKE '%" + id + "%' ORDER BY [Department_Name] DESC";