[英]How to cancel linkButton_Click() Event
我在ASP.Net網站上有一個linkbutton,允許最終用戶向數據庫中添加歌曲。 我想在添加歌曲之前檢查服務器端是否已經存在該歌曲。 這是我的代碼:
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沒有取消方法。 我可以切換到另一種具有取消方法的參數嗎?
謝謝!
您可以嘗試以下操作:
if(cnt ==1)
{
return;
}
希望這會有所幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.