how to call Function of parent form from child form - c#

I am developing an application which consists of 2 forms, parent and child.
In parent form I have a frame. If button 1 of parent form is clicked then child form will open inside the frame. Now what I need is when button 2 of child form is clicked then it should call the Button2_click of the parent form.
Here is my parent form code:
public void MethodToExecute() //call this method
{
UPCCharges.Update();
if (HttpContext.Current.Session["CCost"] != null)
{
TxtCCost.Text = Session["CCost"].ToString();
}
DivCCharges.Visible = false;
IFMCCharge.Visible = false;
}
and this is Form 2 code:
protected void dgCFrom_ItemCommand(Object source, DataGridCommandEventArgs e)
{
UPCFromGrid.Update();
for (int vLoop2 = 0; vLoop2 < gvInner1.Items.Count; vLoop2++)
{
if (TxtTotalCFrom1 != null && TxtTotalCFrom2 != null)
{
TextBox TxtTotalCFrom = (TextBox)dgCFrom.Items[vLoop].FindControl("TxtTotalCFrom");
TextBox TxtTotalCYeild = (TextBox)dgCFrom.Items[vLoop].FindControl("TxtTotalCYeild");
Session["CCost"] = (mobjGenlib.ConvertDecimal(TxtTotalCFrom1.Text) + mobjGenlib.ConvertDecimal(TxtTotalCFrom2.Text)).ToString();
Session["CYeild"] = (mobjGenlib.ConvertDecimal(TxtRecoveryOrigin.Text) - mobjGenlib.ConvertDecimal(TxtTotalCFrom.Text)).ToString();
Session["CName"] = dgCFrom.Items[vLoop].Cells[1].Text;
Session["CJobID"] = HBLGeneralID;
}
}
}
FrmMasterSimulationChartNewUF frm = new FrmMasterSimulationChartNewUF();
frm.MethodToExecute();
}
The error I am getting is:
When keeping break point in the main form function MethodtoExecute getting as Object reference not set to null. But from form1 if execute it was working with no error.
Can anyone please help me solve this?

You should reference to master page
so add something like this line to your Contetnt page design:
<%# MasterType VirtualPath="~/Site.master" %>
and try something like this:
var master = Master as SiteMaster;
SiteMaster mm = new SiteMaster();
mm.MethodToExecute();
master.MethodToExecute();
Let me know the feedback.

Assuming here your "child" form is actually inside an iframe of the "parent" Form, and that both of these pages, are hosted on the same application and domain.
You can attach a javascript click event to Button2 of the child form to then invoke the click event of Button2 on the parent form using parent.document
E.g.
<button id="Button2" onclick="parent.document.getElementById('Button2').click();return false;">Child button 2</button>
Just beware of the actual ID values of your buttons , as they maybe changed due to UpdatePanels etc.

Related

How do I add form on tab control in c#

I have a form page with text boxes and data grid view and other forms that contain a tab control. I want to add the first form tab in the second form. I tried to write the code for the form to appear but it is larger than the tab container and doesn't fit. Only half of the form appears.
This is my code:
private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcMainPage.SelectedIndex == 0)
{
GTOWN.PrintingPage BookInfo = new PrintingPage();
BookInfo.TopLevel = false;
BookInfo.FormBorderStyle = FormBorderStyle.None;
BookInfo.Dock = DockStyle.Fill;
tpSearch.Controls.Add(BookInfo);
BookInfo.Show();
}
}
this is the form
and that is what appears
Set your main FORM as a Container.
yourForm.IsMdiContainer = true;
Then add the child form to the tabPage:
private void tcMainPage_SelectedIndexChanged(object sender, EventArgs e)
{
if (tcMainPage.SelectedIndex == 0)
{
PrintingPage newFrm = new PrintingPage
{
MdiParent = this,
// This set the form parent as the tabClicked
Parent = tcMainPage.TabPages[0]
};
newFrm.Show();
}
}
my tab form work good in the same code
thank you all my code was correct but the problem was in tab property i deleted the tab and add another one and the code is working now
thank you
I face this issue and I create this if may help
public void addform(TabPage tp, Form f)
{
f.TopLevel = false;
//no border if needed
f.FormBorderStyle = FormBorderStyle.None;
f.AutoScaleMode = AutoScaleMode.Dpi;
if (!tp.Controls.Contains(f))
{
tp.Controls.Add(f);
f.Dock = DockStyle.Fill;
f.Show();
Refresh();
}
Refresh();
}
Forms are top-most objects and cannot be placed inside of other containers.
You may want to refactor your code so that the items on your Form are on a UserControl instead. At that point you can then add that UserControl to both a Form and a TabControl
public UserControl myControl(){ /* copy your current view code here */}
public Form myForm(){
Controls.Add(new myControl());
}
public Form myTabbedForm(){
var tabControl = new TabControl();
var page1 = new TabPage();
page1.Controls.Add(new myControl());
tabControl.TabPages.Add(page1);
this.Controls.Add(tabControl);
}

Make User Control hide/show

I have a main page: Main.aspx and 2 user controls User1.ascx and User2.ascx. First, i want User2.ascx to be invisible.I have a hidden value in the main page control. And if value of hidden value is not null then show user2.ascx. I have typed the code in the prerender function on user2.ascx.
Currently, what I try
In Main.aspx
<usercontrol:User1 runat="server" ID="user1control" Visible = "false" />
By this,
In User2, it comes only in pageload event but not in OnPreRender.
I have my all code in OnPreRender
Try something like this
protected void btnToggle_Click(object sender, EventArgs e)
{
string s = btnToggle.Text;
switch (s)
{
case "Hide":
btnToggle.Text = "Show";
break;
case "Show":
btnToggle.Text = "Hide";
break;
}
ucDetails myControl = (ucDetails)Page.LoadControl("~/ucDetails.ascx");
UserControlHolder.Controls.Add(myControl);
myControl.Visible = !myControl.Visible;
}
The other option you can create this on the javascript side. Where you can wrap your usercontrol in a panel and hide and show through javascript function. Hiding and showing through css.
<script type="text/javascript">
function hide() {
document.getElementById("ResultPanel2").style.display = 'none';
}
</script>
I Solved it. I kept a hidden field in main page, and in the load event of main page, I kept a contion that if the value is not null or empty then, user2.visible = true. This worked for me.

Call button click event from one web form to another

I am developing a web application.which contains multiple web forms.
What i need is :-one of my web form contains IFrame (which will open another aspx page with few Textbox and button controls) with close button.
If i click on the button in the child form(Iframe form), once complete its action it should call the close button function of the parent form.
here is the code.
Parent form code
protected void BTNCClose_Click(object sender, EventArgs e)
{
MethodToExecute();
}
public void MethodToExecute() //call this method
{
UPCCharges.Update();
if (HttpContext.Current.Session["CCost"] != null)
{
TxtCCost.Text = Session["CCost"].ToString();
}
if (HttpContext.Current.Session["CYeild"] != null)
{
TxtCYeild.Text = Session["CYeild"].ToString();
}
if (HttpContext.Current.Session["CName"] != null)
{
TxtCName.Text = Session["CName"].ToString();
}
if (TxtCName.Text != "" && TxtCYeild.Text != "" && TxtCCost.Text != "")
{
TxtCrJobId.Text = Session["CJobID"].ToString();
Session.Remove("CCost"); Session.Remove("CYeild");
Session.Remove("CName"); Session.Remove("CJobID");
}
Diva.Visible = false;
IFMC.Visible = false;
}
and this is child form(inside the IFrame)
protected void BTNCCloseChild_Click(object sender, EventArgs e)
{
for (int vLoop2 = 0; vLoop2 < gvInner.Items.Count; vLoop2++)
{
if (TxtTotalCFrom1 != null && TxtTotalCFrom2 != null)
{
TextBox TxtTotalCFrom = (TextBox)gvInner.Items[vLoop2].FindControl("TxtTCFrom");
TextBox TxtTotalCYeild = (TextBox)gvInner.Items[vLoop2].FindControl("TxtTCYeild");
Session["CCost"] = (mobjGenlib.ConvertDecimal(TxtTFrom1.Text) + mobjGenlib.ConvertDecimal(TxtTFrom2.Text)).ToString();
Session["CYeild"] = (mobjGenlib.ConvertDecimal(TxtRO.Text) - mobjGenlib.ConvertDecimal(TxtTFrom.Text)).ToString();
Session["CName"] = gvInner.Items.Count.Items[vLoop2].Cells[1].Text;
Session["CJobID"] = gvInner.Items.Count.Items[vLoop2].Cells[2].Text;
}
}
//after this i want to call that parent form BTNCClose_Click
}
can any one help me to solve this thanks in advance.
This is one way that you can handle
in the BTNCCloseChild_Click event of the child form, add the following code at the end
string script =#"$('the selector of your parent window button',
window.parent.document).click();";
Page.ClientScript.RegisterStartupScript(this.GetType(), "CloseParent", script);
You have to change the 'the selector of your parent window button', to appropreate jquery selector to uniquely select the button of the parent form.
You may have to use window.top instead of window.parent, if there are nested iframes.
Add this line:
ScriptManager.RegisterStartupScript(this, typeof(string), "script", " parent.location.href = parent.location.href;", false);
after this or instead of this
//after this i want to call that parent form BTNCClose_Click
After storing the values in session object it will refresh the parent page and the values will be updated.

Silverlight UserControl

I have 2 user controls. My MainPage control and my edit functions control. My edit control uses WCF to update a database. Once this is done I need to reload the Mainpage control which searched the database to see the changes mad by the edit functions control.
I did this in an asp.net project by redirecting to the MainPage - do you know how this is done in Silverlight.
Ok so i did this by creating a Button at the top of my main page which fires a reload of the page.
I then used the Visual tree helper to get the button and invoke it. I did this by:
Create a class to search Controls:
public static class ControlFinder
{
public static T FindParent(UIElement control) where T : UIElement
{
UIElement p = VisualTreeHelper.GetParent(control) as UIElement;
if (p != null)
{
if (p is T)
return p as T;
else
return ControlFinder.FindParent(p);
}
return null;
}
}
Find the Button within the controls:
UserControl uc = ControlFinder.FindParent(this);
UserControl mainControls = ControlFinder.FindParent(uc);
var PageGrid = VisualTreeHelper.GetChild(mainControls, 0);
var StackPanel = VisualTreeHelper.GetChild(PageGrid, 0);
Button RefreshButton = (Button)VisualTreeHelper.GetChild(StackPanel, 0);
Invoke the button event which fires the refresh
ButtonAutomationPeer buttonAutoPeer = new ButtonAutomationPeer(RefreshButton);
IInvokeProvider invokeProvider = buttonAutoPeer.GetPattern(PatternInterface.Invoke) as IInvokeProvider;
invokeProvider.Invoke();

Dynamic control click event not firing properly

I'm creating a next/previous function for my repeater using pageddatasource. I added the link button control dynamically in my oninit using the following code.
LinkButton lnkNext = new LinkButton();
lnkNext.Text = "Next";
lnkNext.Click += new EventHandler(NextPage);
if (currentPage != objPagedDataSource.PageCount)
{
pnlMain.Controls.Add(lnkNext);
}
So in my initial page_load, the next link comes up fine. There are 5 pages in my objPagedDataSource. currentPage variable is 1.
The "NextPage" event handler looks like this
public void NextPage(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Cookies["PageNum"] == null)
{
HttpCookie cookie = new HttpCookie("PageNum");
cookie.Value = "1";
}
else
{
HttpCookie cookie = HttpContext.Current.Request.Cookies["PageNum"];
cookie.Value = (Convert.ToInt32(cookie.Value) + 1).ToString();
}
this.BindRepeater();
}
So I am incrementing the cookie I am using to track the page number and then rebinding the repeater.
Here is the main issue. The first time I click Next, it works, it goes to Page 2 without any problems. When on Page 2, I click Next, it goes back to Page 1. Seems like the Next event is not wiring up properly. Not sure why, any ideas?
You need to ensure you're adding your dynamic control to the Page every postback. Dynamic controls often cause much pain - in this case it would probably be much easier to declare the "Next" LinkButton in the markup in the normal way, and just set Visible = false when it isn't required.
While you are loading user controls dynamically you must set ID property without ID property Events will not fire.This is My sample code to call user controls dynamically
private void LoadUserControl()
{
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath))
{
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
uc.ID = "uc"; //Set ID Property here
PlaceHolder1.Controls.Add(uc);
}
}

Categories