如何取消linkBut​​ton Click()事件

时间:2014-03-30 17:53:18

标签: c#

我的ASP.Net网站上有一个链接按钮,允许最终用户将歌曲添加到数据库。 如果歌曲在添加之前已经存在,我想检查服务器端。这是我的代码:

protected void linkButtonInsert_Click(object sender, EventArgs e)
    {
        String newArtistName = ((TextBox)myGridView.FooterRow.FindControl("textBoxInsertArtist")).Text;
        String newTitle = ((TextBox)myGridView.FooterRow.FindControl("textBoxInsertTitle")).Text;
        int newGenreId = ((DropDownList)myGridView.FooterRow.FindControl("ddlInsertGenre")).SelectedIndex;
        int newArtistId = -1;
        bool isNewEntry = false;

        //check if new artist exists and get artistId         
        using (SqlConnection con = new SqlConnection(CS))
        {
            //Sql to check if Artist already exists. If true, return id, if false insert new artist into tblArtist and return id.
            SqlCommand cmd = new SqlCommand("if NOT EXISTS (select * from tblArtist where Artist= @newArtistName) INSERT INTO tblArtist (Artist) Output inserted.ID, 'TRUE' as isNewEntry Values(@newArtistName) ELSE Select ID from tblArtist Where Artist = @newArtistName;", con);
            cmd.Parameters.AddWithValue(@"newArtistName", newArtistName);
            con.Open();
            //newArtistId = (int)cmd.ExecuteScalar();
            SqlDataReader rd = cmd.ExecuteReader();
            while (rd.Read())
            {
                newArtistId = Convert.ToInt16(rd["ID"]);
                isNewEntry = Convert.ToBoolean(rd["isNewEntry"]);
            }

        }

        //if isNewEntry == false: check if song already exists: if yes: cancel
        if (!isNewEntry)
        {
            int cnt = -1;
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("Select Count(*) from tblSong WHERE Title = @newTitle AND ArtistId = @newArtistId;", con);
                cmd.Parameters.AddWithValue(@"newTitle", newTitle);
                cmd.Parameters.AddWithValue(@"newArtistId", newArtistId);
                con.Open();
                cnt = (int)cmd.ExecuteScalar();
            }
            if(cnt == 1)
            {

            }

        }

        //insert new song 
        using (SqlConnection con = new SqlConnection(CS))
        {
            SqlCommand cmd = new SqlCommand("Insert into tblSong (Title, ArtistId, GenreId) Values (@newTitle, @newArtistId, @newGenreId);", con);
            cmd.Parameters.AddWithValue(@"newTitle", newTitle);
            cmd.Parameters.AddWithValue(@"newArtistId", newArtistId);
            cmd.Parameters.AddWithValue(@"newGenreId", newGenreId);
            con.Open();
            cmd.ExecuteNonQuery();
        }

        //update grid
        myGridView.DataBind();
    }

现在我想取消该事件,如果它进入

if(cnt ==1) 
{

}

阻止,但我的EventArgs e没有取消方法。我可以切换到另一种类型的Argument,它有一个取消方法,如果有的话?

谢谢你!

1 个答案:

答案 0 :(得分:1)

你可以尝试下面的内容:

if(cnt ==1) 
{
   return;
}

希望这会有所帮助!!