动态用户控件中未触发按钮事件 [英] button event not firing in dynamical user control

查看:58
本文介绍了动态用户控件中未触发按钮事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,先生,我正在动态创建用户控件,但按钮事件未触发,请紧急帮助我.
这是我的代码,如果有人有解决方案,请提前提供帮助.

Hi sir i am creating dynamically user control but button event is not firing please help me it is urgent.
Here is my code please help if any one have the solution thanks in advance.

//AMSAssetAdd.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMSAssetAdd.ascx.cs"Inherits="Website.App_WebControls.App_UserControls.AMSAssetAdd" %>
    <table>
<tr>
<td>
        Asset Name  :
</td>
<td>

        <asp:TextBox ID="TxtAssetName " runat="server"></asp:TextBox>
</td>
</tr>

<tr>
<td>
        Asset Description    :
</td>
<td>

        <asp:TextBox ID="TxtAssetDescription " runat="server"></asp:TextBox>
</td>
</tr>

<tr>
<td>
</td>
<td>
<asp:Button ID="SaveAsset" Text="SAVE" runat="server" onclick="SaveAsset_Click"/>
</td>
</tr>

</table>


//AMSAssetAdd.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.App_UserControls
{
    public partial class AMSAssetAdd : System.Web.UI.UserControl
    {
        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SaveNext_Click(object sender, ImageClickEventArgs e)
        {

         Control uc = LoadControl("AMSBrandAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         PlaceHolder1.Control.Add(uc);
        }
        #endregion


}
}







//AMSBrandAdd.ascx

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AMSBrandAdd.ascx.cs" Inherits="Website.App_WebControls.App_UserControls.AMSBrandAdd" %>
<table>
<tr>
<td>
 Brand Name  :
</td>
<td>
 <asp:TextBox ID="TxtBrandName" runat="server"></asp:TextBox>
</td>
</tr>

<tr>
<td>
</td>
<td>
<asp:Button ID="SaveAsset" Text="SAVE" runat="server" onclick="SaveAsset_Click"/>
</td>
</tr>
</table>


////AMSBrandAdd.ascx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.Objects;
using Swash.BusinessLayer;

namespace Website.App_WebControls.App_UserControls
{
    public partial class AMSBrandAdd : System.Web.UI.UserControl
    {
    }
        protected void SaveNext_Click(object sender, ImageClickEventArgs e)
        {

         Control uc = LoadControl("AMSUnitAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         PlaceHolder1.Control.Add(uc);
        }
}




这是我的aspx页面




This is my aspx page

<%@ Page Title="" Language="C#" AutoEventWireup="true" CodeBehind="AssetMasterDetails.aspx.cs" Inherits="Website.ERP.AMS.AssetMasterDetails" EnableEventValidation="false" ValidateRequest="false" %>
<html>
<head>
<title></title>
</head>
<body>
<div>
 <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</div>
</body>
</html>


//Code Behind
//AssetMasterDetails.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.BusinessLayer;
using Swash.Objects;

namespace Website.ERP.AMS
{
    public partial class AssetMasterDetails : System.Web.UI.Page
    {
     if(!IsPostback)
        {
         LoadControl();
        }
    }

void LoadControl()
    {
         Control uc = LoadControl("AMSAssetAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         PlaceHolder1.Control.Add(uc);

    }
}

推荐答案



我不确定您的意思,但是如果它是动态创建的控件,那么您需要确保在回发时重新创建按钮.否则该事件将不会被触发!

//已更新
Hi,

I''m not sure what you mean, but if it is dynamically created controls, then you need to make sure to re-create the button on postback. Otherwise the event will not get fired!

//Updated
public partial class AssetMasterDetails : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
     //if(!IsPostback)
     //   {
         LoadControl();
     //   }
    }
    void LoadControl()
    {
         Control uc = LoadControl("AMSAssetAdd.ascx");
         PlaceHolder1.Controls.Add(uc);
         //PlaceHoder PlaceHolder1=(PlaceHolder)Page.FindControl("PlaceHolder1");
         //PlaceHolder1.Control.Add(uc);
 
    }
}



您的发布f.x中存在代码错误.您声明了PlaceHolder1,这在LoadControl的作用域中是不可能的,它已经被声明,您将uc两次添加到占位符,代码中的事件名称与ascx中的事件名称不同,后面还有其他名称:-)

问候
Joachim



There are code errors in your posting f.x. you declare PlaceHolder1 which is not possible in the scope in LoadControl, it''s already declared, you add the uc twice to the placeholder, event name in code behind not same as event name in ascx and more :-)

Regards
Joachim


[

请仔细检查一下...

在AMSBrandAdd.ascx.cs中
您已将按钮ID指定为"SaveAsset"
在AMSBrandAdd.ascx.cs
您已编写SaveNext_Click而不是SaveAsset_Click按钮功能


please check it out carefully ...

in AMSBrandAdd.ascx.cs
u have given button id as "SaveAsset"
in AMSBrandAdd.ascx.cs
u have written SaveNext_Click instead of SaveAsset_Click button functionality


<td>
<asp:Button ID="SaveAsset" Text="SAVE" runat="server" onclick="SaveAsset_Click"/>
</td>


这篇关于动态用户控件中未触发按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆