0
在左側,我有3個標籤根據數據庫顯示數據。我希望每當我在gridview中刪除一條記錄時,標籤都會自動刷新。我不想點擊瀏覽器刷新按鈕刷新標籤。我只想刷新標籤。我已經創建了一個名爲UpdateLabel()
的方法,並且我將它放在了grdEvent_RowUpdating
中,它可以工作,只要我編輯它就會刷新標籤。但是,如果我將UpdateLabel()
方法應用於grdEvent_RowDeleting
,它不起作用,爲什麼?幫幫我。幫助如何在gridview中刪除記錄後刷新所有標籤?
點擊刪除按鈕的GridView,這會彈出
點擊在彈出的確認按鈕後。
點擊刷新後,則標籤將刷新
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
UpdateLabels(); // Update labels without refreshing page
string strConnString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from EVENT_ANNOUNCE where getdate() >= STARTDATE and cast(getdate() as Date) <= ENDDATE";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
var events = new List<string>();
if (reader.HasRows)
{
while (reader.Read())
events.Add(reader["EVENTNAME"].ToString());
}
if (events.Count >= 1)
lblEvent1.Text = events[0];
if (events.Count >= 2)
lblEvent2.Text = events[1];
if (events.Count >= 3)
lblEvent3.Text = events[2];
reader.Close();
con.Close();
}
if (Page.IsPostBack == false)
{
bindEventGridView();
}
}
// Update labels without refreshing page
public void UpdateLabels()
{
string strConnString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
string str;
SqlCommand com;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select * from EVENT_ANNOUNCE where getdate() >= STARTDATE and cast(getdate() as Date) <= ENDDATE";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
var events = new List<string>();
if (reader.HasRows)
{
while (reader.Read())
events.Add(reader["EVENTNAME"].ToString());
}
if (events.Count >= 1)
lblEvent1.Text = events[0];
if (events.Count >= 2)
lblEvent2.Text = events[1];
if (events.Count >= 3)
lblEvent3.Text = events[2];
reader.Close();
con.Close();
}
protected void grdEvent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// get row selected by user
int selectedRow = e.RowIndex;
int ID = (int)grdEvent.DataKeys[selectedRow].Value;
// Delete Record
deleteEventRecord(ID);
UpdateLabels();
}
private void deleteEventRecord(int ID)
{
string strConnectionString =
ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "DELETE EVENT_ANNOUNCE WHERE [email protected]";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("ID", ID);
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
lblSuccess.Visible = true;
lblSuccess.Text = "Record deleted";
lblError.Visible = false;
}
else
{
lblError.Visible = true;
lblError.Text = "Update fail";
lblSuccess.Visible = false;
}
bindEventGridView();
myConnect.Close();
}
protected void grdEvent_RowEditing(object sender, GridViewEditEventArgs e)
{
grdEvent.EditIndex = e.NewEditIndex;
bindEventGridView();
}
protected void grdEvent_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int selectedRow = e.RowIndex; //get selected row
// get product id from data key
int id = (int)grdEvent.DataKeys[selectedRow].Value;
// get current grid view row
GridViewRow row = (GridViewRow)grdEvent.Rows[selectedRow];
TextBox eventtype = (TextBox)row.FindControl("txtEventType");
// find text box for txtPrice
TextBox eventname = (TextBox)row.FindControl("txtEventName");
TextBox startdate = (TextBox)row.FindControl("txtStartDate");
TextBox enddate = (TextBox)row.FindControl("txtEndDate");
// Remove $ sign
string strEventType = eventtype.Text;
string strEventName = eventname.Text;
string strStartDate = startdate.Text;
string strEndDate = enddate.Text;
DateTime datStartDate;
DateTime datEndDate;
if (DateTime.TryParseExact(strStartDate, new string[] { "dd/MM/yyyy" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datStartDate)
&&
DateTime.TryParseExact(strEndDate, new string[] { "dd/MM/yyyy" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datEndDate)
)
{
updateEventGridviewRecord(id, strEventType, strEventName, datStartDate, datEndDate);
}
/*
|| DateTime.TryParseExact(strEndDate, new string[] { "dd/MM/yyyy" },
System.Globalization.CultureInfo.InvariantCulture,
System.Globalization.DateTimeStyles.None, out datEndDate
*/
else
{
lblError.Visible = true;
lblError.Text = "Invalid Date";
lblSuccess.Visible = false;
}
UpdateLabels();
}
private void updateEventGridviewRecord(int id, string strEventType, string strEventName, DateTime datStartDate, DateTime datEndDate)
{
try
{
string strConnectionString = ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ConnectionString;
SqlConnection myConnect = new SqlConnection(strConnectionString);
string strCommandText = "UPDATE EVENT_ANNOUNCE SET [EVENTTYPE][email protected], [EVENTNAME][email protected], [STARTDATE][email protected], [ENDDATE][email protected] WHERE [ID][email protected]";
SqlCommand cmd = new SqlCommand(strCommandText, myConnect);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@EVENTTYPE", strEventType);
cmd.Parameters.AddWithValue("@EVENTNAME", strEventName);
cmd.Parameters.AddWithValue("@STARTDATE", datStartDate);
cmd.Parameters.AddWithValue("@ENDDATE", datEndDate);
myConnect.Open();
int result = cmd.ExecuteNonQuery();
if (result > 0)
{
lblSuccess.Visible = true;
lblSuccess.Text = "Record updated!";
lblError.Visible = false;
}
else
{
lblSuccess.Visible = true;
lblError.Text = "Update fail";
lblError.Visible = false;
}
myConnect.Close();
//Cancel Edit Mode
grdEvent.EditIndex = -1;
bindEventGridView();
}
catch
{
lblError.Visible = true;
lblError.Text = "Please Enter Approximate data";
lblSuccess.Visible = false;
}
}