在下面的页面中,我有许多按钮,以及带有编辑按钮的网格。
所有按钮事件都会在我第一次加载页面时触发,但在我单击网格中的编辑按钮后,所有按钮都会停止触发其点击事件。
.aspx代码
<%@ Page Title="" Language="C#" MasterPageFile="~/home/MasterPage.master" AutoEventWireup="true" CodeFile="ContractorTypes.aspx.cs" Inherits="MainForms_ContractorTypes" %>
<%@ Register Src="~/Controls/ResultLabel.ascx" TagPrefix="uc1" TagName="ResultLabel" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
function Clear() {
location.reload(true);
}
function GetConfrim() {
if (confirm("Are You Sure ?")) {
return true;
} else {
return false;
}
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:UpdateProgress AssociatedUpdatePanelID="UpdatePanel1" runat="server" ID="UpdateProgress1">
<ProgressTemplate>
<div class="ajax-loading">
<div></div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="page-content">
<div class="row">
<div class="col-lg-12">
<center>
<asp:Panel ID="ResultPanel" runat="server">
</asp:Panel>
</center>
</div>
<div class="col-md-6">
<div class="panel">
<div class="panel-header bg-primary">
<h3><i class="fa fa-book"></i>Add Contractor Type</h3>
</div>
<div class="panel-content">
<div class="col-md-12">
<div class="col-md-6">
<div class="form-group">
<label class="control-label">
Contractor Type
<asp:HiddenField ID="HdnFieldContractorTypeID" runat="server" />
</label>
<asp:TextBox runat="server" ID="txtBoxContractorTypeName" CssClass="form-control"></asp:TextBox>
</div>
</div>
</div>
<div class="clearfix"></div>
</div>
<div class="panel-footer clearfix">
<div class="pull-right">
<input type="button" value="Reset" onclick="Clear()" class="btn btn-success" />
<asp:Button ID="btnAddContractorType" ValidationGroup="0" runat="server" CssClass="btn btn-primary"
Text="Add" OnClick="btnAddContractorType_Click" />
<asp:Button ID="btnUpdated" ValidationGroup="0" runat="server" CssClass="btn btn-primary"
Text="Update" OnClick="btnUpdated_Click" />
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel fadeIn">
<div class="panel-header bg-success">
<h3><i class="fa fa-search"></i>Contractor Types</h3>
</div>
<div class="panel-content">
<div class="col-md-12">
<div class="input-group">
<label class="control-label">
Search
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Required" ForeColor="Red" ControlToValidate="txtSearch" ValidationGroup="S"></asp:RequiredFieldValidator>
</label>
<asp:TextBox runat="server" CssClass="form-control" ID="txtSearch"></asp:TextBox>
<span class="input-group-btn">
<asp:Button runat="server" ID="btnSearch" OnClick="btnSearch_Click" CssClass="btn btn-warning" Text="Search" Style="margin-top: 25px" ValidationGroup="S" />
</span>
</div>
</div>
<div class="col-md-12">
<asp:GridView runat="server" ID="grdviewContractorTypes" OnRowEditing="grdviewContractorTypes_RowEditing" OnRowCommand="grdviewContractorTypes_RowCommand" DataKeyNames="pk_ContractorTypes_ContractorTypeID" AutoGenerateColumns="false" CssClass="table table-condensed table-bordered table-striped table-responsive">
<Columns>
<asp:BoundField DataField="pk_ContractorTypes_ContractorTypeID" HeaderText="ID" />
<asp:BoundField DataField="ContractorTypeName" HeaderText="Contractor Type" />
<asp:ButtonField CommandName="edit" ImageUrl="~/assets/global/images/shopping/edit.png" ButtonType="Image" ControlStyle-Width="25px" ControlStyle-Height="25px" />
</Columns>
</asp:GridView>
</div>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Contractors;
public partial class MainForms_ContractorTypes : System.Web.UI.Page
{
ASP.controls_resultlabel_ascx ResultLabel = new ASP.controls_resultlabel_ascx();
Contractors.ManageContractorTypes MngContractorTypes = new ManageContractorTypes();
protected void Page_Load(object sender, EventArgs e)
{
//ASP.controls_resultlabel_ascx ResultLabel = new ASP.controls_resultlabel_ascx();
if (!IsPostBack)
{
}
}
protected void btnAddContractorType_Click(object sender, EventArgs e)
{
try
{
if (string.IsNullOrEmpty(txtBoxContractorTypeName.Text))
{
ResultLabel.ResultLabelAttributes("Fill the required fields !", ProjectUserControls.Enums.ResultLabel_Color.Red);
return;
}
string ContractorTypeName = txtBoxContractorTypeName.Text;
if (MngContractorTypes.InsertContractorTypes(ContractorTypeName))
{
txtBoxContractorTypeName.Text= "";
ResultLabel.ResultLabelAttributes("Record inserted successfully !", ProjectUserControls.Enums.ResultLabel_Color.Green);
}
else
{
ResultLabel.ResultLabelAttributes("Account Already Exist !", ProjectUserControls.Enums.ResultLabel_Color.Red);
}
}
catch (Exception)
{
}
finally
{
ResultPanel.Controls.Add(ResultLabel);
}
}
protected void grdviewContractorTypes_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{
if (e.CommandName == "edit")
{
byte ContractorTypeID = Convert.ToByte(grdviewContractorTypes.DataKeys[Convert.ToInt32(e.CommandArgument)].Value);
//HFActID.Value = ID.ToString();
btnAddContractorType.Visible = false;
btnUpdated.Visible = true;
DataTable dt = MngContractorTypes.SelectContractorTypesByContractorTypeID(ContractorTypeID);
DataRow r = dt.Rows[0];
txtBoxContractorTypeName.Text = r["ContractorTypeName"].ToString();
HdnFieldContractorTypeID.Value = r["pk_ContractorTypes_ContractorTypeID"].ToString();
//txtSearch.Text = "Testing...";
//Response.Write("DONE");
}
}
catch (Exception ex)
{
Response.Write(Convert.ToString(ex.Message));
}
}
protected void grdviewContractorTypes_RowEditing(object sender, GridViewEditEventArgs e)
{
// code to edit
}
private void ShowContractorTypes()
{
grdviewContractorTypes.DataSource = MngContractorTypes.SelectContractorTypes();
grdviewContractorTypes.DataBind();
}
protected void btnSearch_Click(object sender, EventArgs e)
{
try
{
DataTable dt = MngContractorTypes.SelectContractorTypesByContractorTypeName(txtSearch.Text.Trim());
if (dt.Rows.Count > 0)
{
grdviewContractorTypes.DataSource = dt;
grdviewContractorTypes.DataBind();
}
else
{
grdviewContractorTypes.EmptyDataText = "No Record Found";
grdviewContractorTypes.DataBind();
}
}
catch (Exception) { }
}
protected void btnUpdated_Click(object sender, EventArgs e)
{
try
{
if (IsPostBack && Page.IsValid)
{
if (string.IsNullOrEmpty(txtBoxContractorTypeName.Text))
{
ResultLabel.ResultLabelAttributes("Fill the required fields !", ProjectUserControls.Enums.ResultLabel_Color.Red);
return;
}
byte ID = Convert.ToByte(HdnFieldContractorTypeID.Value);
string ContractorTypeName = txtBoxContractorTypeName.Text;
if (MngContractorTypes.UpdateContractorTypes(ID, ContractorTypeName))
{
txtBoxContractorTypeName.Text = "";
ShowContractorTypes();
ResultLabel.ResultLabelAttributes("Record updated successfully !", ProjectUserControls.Enums.ResultLabel_Color.Green);
}
else
{
ResultLabel.ResultLabelAttributes("Failed !", ProjectUserControls.Enums.ResultLabel_Color.Red);
}
}
}
catch (Exception) { }
finally
{
ResultPanel.Controls.Add(ResultLabel);
}
}
}