按钮触发两次 - 数据库中插入 2 条记录
在 AutoEventWireup = true 的 MasterPage 中运行。我无法改变这一点。 按钮在数据库中插入新记录。
当按钮按下时,两条记录插入。
<asp:Button ID="AddLab" Text="Add Lab" OnClick="AddLab_OnClick" CssClass="btn" runat="server" />
PageDirective:
AutoEventWireup="true" <- I have tried removing this, setting to false.
PageLoad/PreRender 显示向您显示按钮此处没有执行任何操作:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DbDataReader ddrGrp = rdoGroups();
if (ddrGrp.HasRows)
{
rdoGroup.DataSource = ddrGrp;
rdoGroup.DataBind();
}
ddrGrp.Close();
// preset filter by lab to true
CheckBox FiltLabs = (CheckBox)CommonUI.FindCtrlRecursive(this.Master, "FiltLabs");
FiltLabs.Checked = true;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// nothing yet
rdoLabs.Items.Clear();
DbDataReader ddrLab = rdoUserLabs();
if (ddrLab.HasRows)
{
rdoLabs.DataSource = ddrLab;
rdoLabs.DataBind();
if (CommonUI.strTest((string)Session["rdoLabs"]))
{
if (Convert.ToInt32(Session["rdoLabs"]) > 0)
{
rdoLabs.SelectedValue = (string)Session["rdoLabs"];
}
}
}
ddrLab.Close();
// get group-lab mappings
cboGroupLab.Items.Clear();
DbDataReader ddrGroupLab = cboGroupLabsMap();
if (ddrGroupLab != null)
{
if (ddrGroupLab.HasRows)
{
cboGroupLab.DataSource = ddrGroupLab;
cboGroupLab.DataBind();
}
}
}
protected void AddLab_OnClick(object sender, EventArgs e)
{
LabAdmn labAdd = new LabAdmn();
TextBox txtLab = (TextBox)CommonUI.FindCtrlRecursive(this.Master, "txtLab");
CheckBox Active = (CheckBox)CommonUI.FindCtrlRecursive(this.Master, "cboLabActive");
string[] strArr = new string[] { "lab_id" };
labAdd.LabName = txtLab.Text;
labAdd.Active = (bool)Active.Checked;
// AddLab is where record is fired...see below...
Hashtable ht = labAdd.AddLab(labAdd, strArr);
ht = labAdd.AddLab(labAdd, strArr);
if (ht != null)
{
HiddenField hLabId = (HiddenField)CommonUI.FindCtrlRecursive(this.Master, "hLabId");
hLabId.Value = Convert.ToString(GetHTParm(ht, strArr[0]));
}
}
#endregion AddLab_OnClick
#region AddLab
public Hashtable AddLab(LabAdmn labAdmn, string[] colOutputNames)
{
DAL myDal = new DAL(DBType, DB);
Hashtable ht = new Hashtable();
DAL.Parameters[] parms = new DAL.Parameters[]
{
new DAL.Parameters("driver_id","A",ParameterDirection.Input),
new DAL.Parameters("lab_id",labAdmn.LabId,ParameterDirection.InputOutput),
new DAL.Parameters("active",labAdmn.Active,ParameterDirection.Input),
new DAL.Parameters("lab_name",labAdmn.LabName,ParameterDirection.Input)
};
CommandType cmdType = CommandType.StoredProcedure;
string cmdText = "asp_Labs_Admin";
ht = myDal.ExecuteQueryOutput(cmdType, cmdText, colOutputNames, parms, true);
return ht;
}
#endregion AddLab
我已经读到 onclick 和 runat 会导致双重处理。如果我删除 runat,该按钮将不会显示,因为它是服务器端按钮。如果删除 OnClick 事件,则无论 AutoEventWireup 是 true、false 还是已删除,该事件都不会触发。
Running in MasterPage with AutoEventWireup = true. I cannot change this.
Button inserts new record in database.
When button hits, two records insert.
<asp:Button ID="AddLab" Text="Add Lab" OnClick="AddLab_OnClick" CssClass="btn" runat="server" />
PageDirective:
AutoEventWireup="true" <- I have tried removing this, setting to false.
PageLoad/PreRender show to show you nothing being done here with button:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DbDataReader ddrGrp = rdoGroups();
if (ddrGrp.HasRows)
{
rdoGroup.DataSource = ddrGrp;
rdoGroup.DataBind();
}
ddrGrp.Close();
// preset filter by lab to true
CheckBox FiltLabs = (CheckBox)CommonUI.FindCtrlRecursive(this.Master, "FiltLabs");
FiltLabs.Checked = true;
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
// nothing yet
rdoLabs.Items.Clear();
DbDataReader ddrLab = rdoUserLabs();
if (ddrLab.HasRows)
{
rdoLabs.DataSource = ddrLab;
rdoLabs.DataBind();
if (CommonUI.strTest((string)Session["rdoLabs"]))
{
if (Convert.ToInt32(Session["rdoLabs"]) > 0)
{
rdoLabs.SelectedValue = (string)Session["rdoLabs"];
}
}
}
ddrLab.Close();
// get group-lab mappings
cboGroupLab.Items.Clear();
DbDataReader ddrGroupLab = cboGroupLabsMap();
if (ddrGroupLab != null)
{
if (ddrGroupLab.HasRows)
{
cboGroupLab.DataSource = ddrGroupLab;
cboGroupLab.DataBind();
}
}
}
protected void AddLab_OnClick(object sender, EventArgs e)
{
LabAdmn labAdd = new LabAdmn();
TextBox txtLab = (TextBox)CommonUI.FindCtrlRecursive(this.Master, "txtLab");
CheckBox Active = (CheckBox)CommonUI.FindCtrlRecursive(this.Master, "cboLabActive");
string[] strArr = new string[] { "lab_id" };
labAdd.LabName = txtLab.Text;
labAdd.Active = (bool)Active.Checked;
// AddLab is where record is fired...see below...
Hashtable ht = labAdd.AddLab(labAdd, strArr);
ht = labAdd.AddLab(labAdd, strArr);
if (ht != null)
{
HiddenField hLabId = (HiddenField)CommonUI.FindCtrlRecursive(this.Master, "hLabId");
hLabId.Value = Convert.ToString(GetHTParm(ht, strArr[0]));
}
}
#endregion AddLab_OnClick
#region AddLab
public Hashtable AddLab(LabAdmn labAdmn, string[] colOutputNames)
{
DAL myDal = new DAL(DBType, DB);
Hashtable ht = new Hashtable();
DAL.Parameters[] parms = new DAL.Parameters[]
{
new DAL.Parameters("driver_id","A",ParameterDirection.Input),
new DAL.Parameters("lab_id",labAdmn.LabId,ParameterDirection.InputOutput),
new DAL.Parameters("active",labAdmn.Active,ParameterDirection.Input),
new DAL.Parameters("lab_name",labAdmn.LabName,ParameterDirection.Input)
};
CommandType cmdType = CommandType.StoredProcedure;
string cmdText = "asp_Labs_Admin";
ht = myDal.ExecuteQueryOutput(cmdType, cmdText, colOutputNames, parms, true);
return ht;
}
#endregion AddLab
I've read that onclick and runat cause double processing. If I remove runat, the button won't show since it is a server side button. If remove the OnClick event, the event never fires regardless if AutoEventWireup is true, false, or removed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来您正在调用“AddLab”方法两次:
It looks like you are calling the "AddLab" method twice:
如果您没有将处理程序两次附加到按钮,请查看代码中的某个位置。也许在设计器中附加了一个事件处理程序。也许在你的初始化代码中的某个地方。
尝试删除带有按钮的代码并再次放置它,而不在标记中添加 onclick 属性。将您的页面或控件添加到以下“oninit”中
但首先,尝试删除标记的属性,然后重试,如果它仍然触发两次
Please look somewhere in your code, if you are not attaching a handler to the button twice. Maybe in the Designer there is an event handler attached. Maybe somewhere in your init code.
Try remove your code with the button and place it again, without the onclick attribute in the markup. add in your page or control into the "oninit" following
But first, try to remove the attribute of the markup and try again, if it still fires twice
您可以将 AddLab_OnClick() 方法重命名为其他名称并将其分配到 OnClick="" 处理程序中吗?我认为 AutoEventWireup 正在触发第一个事件,第二个事件是由于 OnClick 分配再次导致的。
另外,请尝试 Luke 的建议。这将证明事件分配..
Can you rename the AddLab_OnClick() method to something else and assign that in OnClick="" handler? I am thinking AutoEventWireup is triggering the first event and the second one is due to OnClick assignment again..
Also, Try what Luke has suggested. That will prove the event assignments..