动态Web用户控制 [英] Dynamic Web USer Control

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

问题描述

大家好,

我已经为(Change Password.ascx)创建了一个Web用户控件,并为.aspx页面(Home.aspx)创建了一个Web用户控件,然后使用拖放将Web用户控件添加到页面中.而且工作正常.

现在,我想将用户控件动态添加到页面.喜欢

HI All,

I have created one web user control for (Change Password.ascx ) and one .aspx page (Home.aspx) and then add the web-user control to a page using Drag and Drop. and it is working fine.

Now I want to add the user control dynamically to a page. like

Control ctrl=LoadControl("ChangePass.ascx");

PlaceHolder1.Controls.Add(ctrl);



我可以将控件添加到Home.aspx,但是Web用户控件中放置的按钮不会触发其click事件.

如果未动态添加用户控件,则不会触发按钮的单击事件.

我在Google上搜索了此内容,但未得到适当的回复.

请帮助我解决此问题.

谢谢

这是我的用户控件代码(ChangePass.ascx)

===================================



I am able to add the control to the Home.aspx But the button placed inside Web User Control not firing its click event.

Click event of the button is not fired If I not add the user control dynamically.

I google this thing but not getting proper reply.

Please help me to solve this issue.

Thanks

Here is my code for User Control (ChangePass.ascx")

=====================================

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="ChangePass.ascx.cs" Inherits="ChangePass" %>

 <table class="style1">
 <tr>

 <td>
New Password
 </td>

 <td>
 <asp:TextBox ID="txtNewPassword" runat="server" Width="218px" TextMode="Password"></asp:TextBox>
 </td>

 <td>
 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtNewPassword"

 ErrorMessage="Enter New Password"></asp:RequiredFieldValidator>
 </td>

 </tr>

 <tr>

 <td>
Confirm Password
 </td>

 <td>
 <asp:TextBox ID="txtCNewPassword" runat="server" Width="218px" TextMode="Password"></asp:TextBox>
 </td>

 <td>
 <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtNewPassword"

 ControlToValidate="txtCNewPassword" Display="Dynamic" ErrorMessage="Password and Confirm Password does not match"></asp:CompareValidator>

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCNewPassword"

Display="Dynamic" ErrorMessage="Enter Confirm Password"></asp:RequiredFieldValidator>
</td>

</tr>
<tr>

<td colspan="3">
<asp:Label ID="Label1" runat="server" Font-Bold="True" ForeColor="#990000"></asp:Label>
 </td>

 </tr>

 <tr>
 <td colspan="3">
 <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />
 </td>

 </tr>

</table>



===========================================
Web用户控件.cs文件的代码(ChangePass.ascx.cs)



==============================================
Code for Web User Control .cs File (ChangePass.ascx.cs)

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class ChangePass : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
       Class1 objCls = new Class1();
        int i;
        i = objCls.UpdateEmployeePassword(Convert.ToInt32(Session["EmpId"].ToString()), txtNewPassword.Text);
        if (i > 0)
        {
            Label1.Text = "Password Change Successfully";
        }
        else
        {
        }
    }
  }
}



n主页上放置一个按钮,然后单击一个按钮,然后单击我加载Wen用户控件"
在按钮的单击事件上:-



n Home page I palce a Button and on Button Click I Load the Wen User Control
on Click Event of Button:-

Control ctrl=LoadControl("ChangePass.ascx");
PlaceHolder1.Controls.Add(ctrl);


==============================================
它加载了控件,但是用于更改密码的click事件不是File.

请帮助解决此问题.
谢谢


================================================
It load the control but click event for changing the password is not File.

Please help to solve this.
Thanks

推荐答案

不确定是什么问题,我使用了您的代码,可以动态地从页面加载事件中加载用户控件,当我单击保存按钮时,完美地触发了该事件.

但是,如果要同时加载控件并触发按钮事件,则可能需要处理页面上的验证器,当它们的验证结果为false时,将提交用户控件.我仍然不确定为什么要在加载的同时启动它,但是先解除验证器的武装,然后您就可以很好地执行启动事件.
not sure what exactly your problem, I have used your code and dynamically able to load the user control from page load event and when i click the save button it is perfectly firing the event.

but if you want to load the control and fire the button event simultaneously, then you need to probably work on the validators on the page, when they evaluated to false why user control will be submitted. I am still not sure why you want to fire it at the same time of loading, but disarm the validator stuff then you should be good with fire event.


如果我没有更正我的话.''不能正确解决您的问题!

在您的代码中,好像您在aspx页面中的Button的Click_Event上动态添加了一个UserControl,但是问题在于,一旦更改密码UserControl将被动态加载,并且当您尝试单击其Button(在USerControl内部)时,它将再次调用父页面的Page_Load,因此您的父页面将找不到ChangePassword的任何引用,因为它没有(动态)加载到那里,最终您的Button_Click也不会被标识.

如果是上述情况,那么您需要编码一些逻辑,以确保您的UserControl也已在父页面加载中注册/加载!
Correct me if I didn''t get your issue correctly!

In your code it seems like you are adding dynamically a UserControl on Click_Event of a Button in your aspx page but problem is once your Change Password UserControl will be dynamically loaded and when you will try to click its Button (inside the USerControl), it will again invoke Page_Load of your parent page and thus your parent page will not find any reference of your ChangePassword because it is not loaded (dynamically) there and finally your Button_Click will not be identified.

If above is the actual case then you need to code some logic to ensure that your UserControl is also registered / loaded in your parent page load!!


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

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