1

私は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最初の try catch をコメントアウトしても、2 番目の try catch を起動できません。

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 catch です。

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>";
            }
4

1 に答える 1

0

SelectCommand部門エリアにパーセント記号を追加する必要があります。

正しい形式に従ってください:SqlDataSource1.SelectCommand = "SELECT * FROM [table2] " + "WHERE Department_Name LIKE '%" + id + "%' ORDER BY [Department_Name] DESC";

于 2013-10-09T14:12:28.590 に答える