如何在插入gridview之前檢查我的插入代碼是否在按鈕點擊之下。在插入gridview之前查找重複
if (Session["dtInSession"] != null)
dt1 = (DataTable)Session["dtInSession"];
DataRow dr = dt1.NewRow();
dr["AccessoryName"] = drpAccessories.SelectedItem;
dr["ConditionName"] = drpConditions.SelectedItem;
dt1.Rows.Add(dr);
Session["dtInSession"] = dt1;
ViewState["CurrentTable"] = dt1;
grvList.DataSource = dt1;
grvList.DataBind();
我試過這種方式,但肯定它只會檢查最後選擇的項目,而不是其他已插入的行。
if (grvList.Rows.Count > 0)
{
foreach (GridViewRow gr in grvList.Rows)
{
accessorucheck = ((Label)gr.FindControl("txtAccessories")).Text;
if (accessorucheck == drpAccessories.Text)
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation1", "<script language='javascript'>alert('Accessory Alredy Added, Please change the condition if needed.')</script>");
}
else
{
if (Session["dtInSession"] != null)
dt1 = (DataTable)Session["dtInSession"];
DataRow dr = dt1.NewRow();
dr["AccessoryName"] = drpAccessories.SelectedItem;
dr["ConditionName"] = drpConditions.SelectedItem;
dt1.Rows.Add(dr);
Session["dtInSession"] = dt1;
grvList.DataSource = dt1;
grvList.DataBind();
}
}
}
它只會幫助您顯示不同,而不會在插入時顯示任何錯誤消息。 –
謝謝ihtsham。它現在與你的代碼一起工作。非常感謝您的幫助。 – NAJEEB