为什么按钮没有击中事件?我放了一个断点,从调试开始但是当我点击它没有点击事件但其他按钮呢?为什么?
我还发布了完整的代码,以防你想看一下。
事件:
protected void btnUpdate_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);
}
}
按钮:
<asp:Button ID="btnUpdate" ValidationGroup="0" runat="server" CssClass="btn btn-warning"
Text="Update" OnClick="btnUpdate_Click" />
完整代码:
<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="btnUpdate" runat="server" CssClass="btn btn-warning"
Text="Update" OnClick="btnUpdate_Click" Visible="false" />
</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>
服务器代码:
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;
btnUpdate.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
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
try
{
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);
}
}
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) { }
}
}
答案 0 :(得分:1)
您是否从其他页面/应用程序复制了此方法?
如果是那么它将无效。
您需要删除分配给该按钮的事件和事件名称,然后转到设计视图并转到按钮事件属性转到onClick事件双击它旁边,它将生成事件并自动为该按钮分配事件名称
如果否,
尝试Clean
您的解决方案,然后再试一次。
请参阅this文章
答案 1 :(得分:0)
尝试将访问修饰符从protected更改为public,这将根据您的类结构停止调用该方法。
有关访问修饰符的详情,请参阅此处: https://msdn.microsoft.com/en-us/library/ms173121.aspx
答案 2 :(得分:0)
尝试启用Button的AutoPostBack属性。
<asp:Button ID="btnUpdate" ValidationGroup="0" runat="server" CssClass="btn btn-warning" Text="Update" OnClick="btnUpdate_Click" AutoPostBack="true" />