I have a situation where I will be adding/updating one type of record from an ascx. In addition, the record being manipulated can have a dependent set of record(s). There is a second ascx which will list these dependent records (if any) and allow addition
of new records or modification of existing ones.
My quandry is: if the primary record is new, I do not want to allow entry of any dependent records until I have created the primary record and retrieved its key. The easiest way I can think of to do this is to make the add button inactive until I have
a primary key. My problem is that I don't know how to do this.
First ascx is named AddUpdateClientControl.ascx; second ascx is AddUpdatePhoneControl.ascx. Each ascx will have its own btnAdd control. In the first ascx file, I'd like to activate the btnAdd in the second ascx.
This looks great and it does exactly what I need. I understand the code and can easily change it to reflect my objects. I will need to pass the primary key of the client record created in uc1 to uc2. I think I can do that by setting the value of an object
named txtClntPK in uc2 from uc1 after I add the new client in uc1.
Beside 2nd man's nice idea,You can also use an event do deal with the program:
public partial class MyControl1 : System.Web.UI.UserControl
{
private Control RecursiveFindControl(string id,Control uc)
{
UserControl _uc = null;
foreach (Control item in uc.Controls)
{
if (item is UserControl && uc.ID == id)
{
_uc = item as UserControl;
break;
}
if (item.HasControls())
{
RecursiveFindControl(id, item);
}
}
return _uc;
}
public event Action<UserControl> OtherUserControlFinding;
public void FindControl(string id)
{
UserControl uc = RecursiveFindControl(id, this.Page) as UserControl;
if (uc != null)
{
//Raise the event to report to the client that control is found……
OtherUserControlFinding(uc);
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
}
When I began the debug process, I error out at the bolded line above with the message that the object is null or has not been defined. When I do a quick watch on (ContractSystem.AddUpdateClientControl)Page.FindControl("AddUpdateClientControl"), I discover
that it is null. I'm pretty sure that is incorrect because the control is on the page. Just to be sure, I tried both cases: one where the Request.QueryString["CLNT_PK"] had a value and one where it was null. Same results.
Please note that the 2 controls are in different namespaces. I didnot write the original project and am adding Linq and other 'stuff' to it. When I created the new Phone control, it created it in the current project name space. If this is the problem,
can I simply change the namespace designator or should I just recreate the client control?
Thanks so much for you help to this point. What haven't I done that I need to do? This is crucial to the project. I will be putting the phone control on many of the other pages.
I ment to say it's NOT working. Please see my post above. I did redo the AddUpdateClientControl in the same namespace as the phone control and it made no difference at all.
I'm not savvy enough to figure out why it's not finding the control on the page although the syntax appears to be correct.
I'm not sure how to use your solution. In my case I want to use an object's value (textBox.Text) in uc2. I"m not sure where I would put your code or just how to use it.
MParkhouse
Member
22 Points
93 Posts
Activating one ascx from another
May 16, 2012 12:38 AM|LINK
I have a situation where I will be adding/updating one type of record from an ascx. In addition, the record being manipulated can have a dependent set of record(s). There is a second ascx which will list these dependent records (if any) and allow addition of new records or modification of existing ones.
My quandry is: if the primary record is new, I do not want to allow entry of any dependent records until I have created the primary record and retrieved its key. The easiest way I can think of to do this is to make the add button inactive until I have a primary key. My problem is that I don't know how to do this.
First ascx is named AddUpdateClientControl.ascx; second ascx is AddUpdatePhoneControl.ascx. Each ascx will have its own btnAdd control. In the first ascx file, I'd like to activate the btnAdd in the second ascx.
All help will be appreciated.
cninjas
Contributor
4868 Points
851 Posts
Re: Activating one ascx from another
May 16, 2012 06:00 AM|LINK
hi
I tried the same scenerio and i hope you are trying to do the same.
I am just going to paste a set of code here.
Html Page of usercontrol1
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddUpdateClientControl.ascx.cs" Inherits="uc_AddUpdateClientControl" %> <%@ Register Src="AddUpdatePhoneControl.ascx" TagName="AddUpdatePhoneControl" TagPrefix="uc2" %> <table width="100%"> <tr> <td> First Name </td> <td> <asp:TextBox runat="server" ID="txtFirstName"></asp:TextBox> </td> </tr> <tr> <td> Last Name </td> <td> <asp:TextBox runat="server" ID="txtLastName"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button runat="server" ID="btnAdd" Text="Add" onclick="btnAdd_Click" /> </td> <td> <asp:Button runat="server" ID="btnReset" Text="Reset" /> </td> </tr> </table>Add Click Event of the uesrcontrol1
protected void btnAdd_Click(object sender, EventArgs e) { uc_AddUpdatePhoneControl uc_AddUpdatePhoneControl1 = (uc_AddUpdatePhoneControl)Page.FindControl("AddUpdatePhoneControl1"); uc_AddUpdatePhoneControl1.ShowControls = true; }Html page of the usercontrol2
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AddUpdatePhoneControl.ascx.cs" Inherits="uc_AddUpdatePhoneControl" %> <table width="100%"> <tr> <td> Address 1 </td> <td> <asp:TextBox runat="server" ID="txtAddress1"></asp:TextBox> </td> </tr> <tr> <td> Address 2 </td> <td> <asp:TextBox runat="server" ID="txtAddress2"></asp:TextBox> </td> </tr> <tr> <td> <asp:Button runat=server ID="btnSavePhone" Text="Save Phone" /> </td> <td> <asp:Button runat=server ID="btnSaveReset" Text="Reset" /> </td> </tr> </table>The following is the code behind of the usercontrol.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class uc_AddUpdatePhoneControl : System.Web.UI.UserControl { bool _showcontrols = false; public bool ShowControls { get { return _showcontrols; } set { _showcontrols = value; EnableDisableControls(_showcontrols); } } private void EnableDisableControls(bool _showcontrols) { txtAddress1.Enabled = _showcontrols; txtAddress2.Enabled = _showcontrols; btnSavePhone.Enabled = _showcontrols; btnSaveReset.Enabled = _showcontrols; } protected void Page_Load(object sender, EventArgs e) { } }<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <%@ Register Src="uc/AddUpdateClientControl.ascx" TagName="AddUpdateClientControl" TagPrefix="uc1" %> <%@ Register Src="uc/AddUpdatePhoneControl.ascx" TagName="AddUpdatePhoneControl" TagPrefix="uc2" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body style="height: 310px"> <form id="form1" runat="server"> <table width="100%"> <tr> <td> <uc1:AddUpdateClientControl ID="AddUpdateClientControl1" runat="server" /> </td> <td> <uc2:AddUpdatePhoneControl ID="AddUpdatePhoneControl1" ShowControls=false runat="server" /> </td> </tr> </table> </form> </body> </html>Hope it helps.thanks.
Niranjan
MParkhouse
Member
22 Points
93 Posts
Re: Activating one ascx from another
May 16, 2012 09:39 PM|LINK
This looks great and it does exactly what I need. I understand the code and can easily change it to reflect my objects. I will need to pass the primary key of the client record created in uc1 to uc2. I think I can do that by setting the value of an object named txtClntPK in uc2 from uc1 after I add the new client in uc1.
Does that make sense?
Thanks so much!
cninjas
Contributor
4868 Points
851 Posts
Re: Activating one ascx from another
May 17, 2012 04:17 AM|LINK
Hi
Yes exactly. Try it out and let us know in case if you are struck. Happy coding !!
Niranjan
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Activating one ascx from another
May 17, 2012 11:57 PM|LINK
Hello MParkhouse:)
Beside 2nd man's nice idea,You can also use an event do deal with the program:
public partial class MyControl1 : System.Web.UI.UserControl { private Control RecursiveFindControl(string id,Control uc) { UserControl _uc = null; foreach (Control item in uc.Controls) { if (item is UserControl && uc.ID == id) { _uc = item as UserControl; break; } if (item.HasControls()) { RecursiveFindControl(id, item); } } return _uc; } public event Action<UserControl> OtherUserControlFinding; public void FindControl(string id) { UserControl uc = RecursiveFindControl(id, this.Page) as UserControl; if (uc != null) { //Raise the event to report to the client that control is found…… OtherUserControlFinding(uc); } } protected void Page_Load(object sender, EventArgs e) { } }MParkhouse
Member
22 Points
93 Posts
Re: Activating one ascx from another
May 18, 2012 03:51 PM|LINK
Well, I'm in the debug phase right now and it's now working. Here's what I have:
Calling web page:
<%@ Page Title="" Language="C#" MasterPageFile="~/Master/ContractsAndMore.Master"
AutoEventWireup="true" CodeBehind="AddUpdateClient.aspx.cs" Inherits="ContractSystem.AddUpdateClient" %>
<%@ Register Src="../AddUpdateClientControl.ascx" TagName="AddUpdateClientControl"
TagPrefix="uc1" %>
<%@ Register Src="../AddUpdatePhoneControl.ascx" TagName="AddUpdatePhoneControl"
TagPrefix="uc2" %>
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<div class="contentHolder">
<h1>
Add/Update Client</h1>
<uc1:AddUpdateClientControl ID="AddUpdateClientControl1" runat="server" />
<br />
<uc2:AddUpdatePhoneControl ID="AddUpdatePhoneControl1" runat="server" />
</div>
</asp:Content>
AddUpdateClientControl (partial code):
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddUpdateClientControl.ascx.cs" Inherits="ContractSystem.AddUpdateClientControl" %>
<table>
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="lblAddPerson" runat="server" Text="Add Contact"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtCLNT_PK" runat="server" Visible="false" />
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
<asp:TextBox ID="txtName" runat="server" />
<asp:RequiredFieldValidator ID="reqClientName" runat="server"
ErrorMessage="Name Required" ControlToValidate="txtName"></asp:RequiredFieldValidator>
</td>.
.
.
Code behind for above:
namespace ContractSystem
{
public partial class AddUpdateClientControl : System.Web.UI.UserControl
{
public TextBox txtClntPK
{
get
{
return this.txtCLNT_PK;
}
}
//protected ContractsAndMore.AddUpdatePhoneControl myPhoneControl;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["CLNT_PK"] != null)
{
txtCLNT_PK.Text = Request.QueryString["CLNT_PK"];
loadClient();
btnAddUpdate.Text = "Update";
ContractsAndMore.AddUpdatePhoneControl AddUpdatePhoneControl1 = (ContractsAndMore.AddUpdatePhoneControl)Page.FindControl("AddUpdatePhoneControl1");
AddUpdatePhoneControl1.ShowControls = true;
}
}
}
.
.
protected void btnAddUpdate_Click(object sender, EventArgs e)
{
// To raise events
// Check if event is not null
//if (AddNew != null)
// AddNew(this, new EventArgs());
if (btnAddUpdate.Text == "Add")
{
saveClient();
ContractsAndMore.AddUpdatePhoneControl AddUpdatePhoneControl1 = (ContractsAndMore.AddUpdatePhoneControl)Page.FindControl("AddUpdatePhoneControl1");
AddUpdatePhoneControl1.ShowControls = true;
}
else
{
updateClient();
}
// Response.Redirect("ViewClient.aspx");
}
AddUpdatePhoneControl.ascx:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="AddUpdatePhoneControl.ascx.cs" Inherits="ContractsAndMore.AddUpdatePhoneControl" %>
<%@ Reference VirtualPath="AddUpdateClientControl.ascx" %>
<table>
<tr>
<td colspan="3">
<asp:TextBox ID="txtClntPK" runat="server" Visible="false" />
<h1>Phone Numbers</h1>
</td>
</tr>
<tr>
<td colspan="3">
<asp:GridView ID="grdPhone" runat="server" Caption="View Phone Numbers" DataKeyNames="PHON_PK" AutoGenerateColumns="false"
EmptyDataText="No Phone numbers on file" OnRowEditing="grdPhone_RowEditing" OnRowDeleting="grdPhone_RowDeleting" >
<Columns>
<asp:BoundField DataField="PHON_PK" Visible="false" />
<asp:CommandField ShowEditButton="true" ShowDeleteButton="true" />
<asp:BoundField DataField="PHON_NUMBER" HeaderText="Number" />
<asp:BoundField DataField="PHON_EXT" HeaderText="Extension" />
<asp:BoundField DataField="LKUP_DISPLAY_NAME" HeaderText="Type" />
<asp:BoundField DataField="PHON_NAME" HeaderText="Description" />
<asp:BoundField DataField="PHON_PRIMARY" HeaderText="Primary" />
<asp:BoundField DataField="PHON_ACTIVE" HeaderText="Active" />
</Columns>
</asp:GridView>
</td>
</tr>
<tr >
<td colspan="3"> </td>
</tr>
<tr >
<td colspan="3"><h1>Add/Modify a Phone Number</h1>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblPhone" runat="server" Text="Phone Number:"/>
<asp:TextBox ID="txtPhone" runat="server" />
</td>
<td>
<asp:Label ID="lblExtension" runat="server" Text="Extension:"/>
<asp:TextBox ID="txtExtension" runat="server" />
</td>
<td>
<asp:Label ID="lblPhoneType" runat="server" Text="Type" />
<asp:DropDownList ID="ddPhonType" runat="server" DataTextField="LKUP_DISPLAY_NAME" DataValueField="LKUP_PK" />
</td>
</tr>
<tr>
<td>
<asp:Label ID="lblPrimary" runat="server" Text="Primary:"/>
<asp:TextBox ID="txtPrimary" runat="server" />
</td>
<td>
<asp:Label ID="lblDescription" runat="server" Text="Description:"/>
<asp:TextBox ID="txtDescription" runat="server" />
</td>
<td>
<asp:Label ID="lblActive" runat="server" Text="Active:"/>
<asp:TextBox ID="txtActive" runat="server" />
</td>
</tr>
<tr>
<td colspan="3"; align="center">
<asp:Button ID="btnAdd" runat="server" Text="ADD" OnClick="btnAdd_OnClick" />
</td>
</tr>
</table>
Code behind:
namespace ContractsAndMore
{
public partial class AddUpdatePhoneControl : System.Web.UI.UserControl
{
bool _showcontrols = false;
public bool ShowControls
{
get
{
return _showcontrols;
}
set
{
_showcontrols = value;
EnableDisableControls(_showcontrols);
}
}
private void EnableDisableControls(bool _showcontrols)
{
txtClntPK.Enabled = _showcontrols;
txtPhone.Enabled = _showcontrols;
ddPhonType.Enabled = _showcontrols;
btnAdd.Enabled = _showcontrols;
// btnSaveReset.Enabled = _showcontrols;
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
ContractSystemDataContext db = new ContractSystemDataContext(connectionString);
//calling page should set txtClntPK to either '' or the client primary key
ContractSystem.AddUpdateClientControl ctrlB = (ContractSystem.AddUpdateClientControl)Page.FindControl("AddUpdateClientControl");
if (ctrlB.txtClntPK != null)
{
TextBox txtClntPK = ctrlB.txtClntPK;
.
When I began the debug process, I error out at the bolded line above with the message that the object is null or has not been defined. When I do a quick watch on (ContractSystem.AddUpdateClientControl)Page.FindControl("AddUpdateClientControl"), I discover that it is null. I'm pretty sure that is incorrect because the control is on the page. Just to be sure, I tried both cases: one where the Request.QueryString["CLNT_PK"] had a value and one where it was null. Same results.
Please note that the 2 controls are in different namespaces. I didnot write the original project and am adding Linq and other 'stuff' to it. When I created the new Phone control, it created it in the current project name space. If this is the problem, can I simply change the namespace designator or should I just recreate the client control?
Thanks so much for you help to this point. What haven't I done that I need to do? This is crucial to the project. I will be putting the phone control on many of the other pages.
Thanks in advance.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Activating one ascx from another
May 18, 2012 11:48 PM|LINK
Congratulation!Welcome to our fourm next time to share us your nice solution as well as chat with us about the technology of ASP.NET……
Reguards!
MParkhouse
Member
22 Points
93 Posts
Re: Activating one ascx from another
May 19, 2012 12:04 AM|LINK
I ment to say it's NOT working. Please see my post above. I did redo the AddUpdateClientControl in the same namespace as the phone control and it made no difference at all.
I'm not savvy enough to figure out why it's not finding the control on the page although the syntax appears to be correct.
Thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Activating one ascx from another
May 19, 2012 02:19 AM|LINK
sorry:(
Have you tried my way?
MParkhouse
Member
22 Points
93 Posts
Re: Activating one ascx from another
May 21, 2012 10:04 PM|LINK
Decker,
I'm not sure how to use your solution. In my case I want to use an object's value (textBox.Text) in uc2. I"m not sure where I would put your code or just how to use it.
Sorry to be so dense.
Thanks