Click here to Skip to main content
15,879,474 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
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);


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")

=====================================
XML
<%@ 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>


==============================================
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 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);

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

Please help to solve this.
Thanks
Posted
Updated 7-Apr-11 5:26am
v2
Comments
prashant1111 7-Apr-11 7:31am    
Means in web user control I have 2 text box ( 1 for password and 1 for confirm password) and 1 button and click on that button password is changed

on Home.aspx I put a button and on click of that button I want to load the control and change the password

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

It loads the control but click event is not fired

Please help me.

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.
 
Share this answer
 
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!!
 
Share this answer
 
v2
Comments
Sandeep Mewara 26-Apr-11 4:57am    
Hey, thee was an issue and your answer got deleted on other place (About globalization). Please re-add it. Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900