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.
Related
I know about the FindControl BUT my button is inside a Login control which is inside a LoginView control,below is my code but its not working!!
Login log = LoginView1.FindControl("Login1") as Login;
Button logButton = log.FindControl("LoginButton") as Button;
this.Form.DefaultButton = logButton.UniqueID;
Th error I get is
Object reference not set to an instance of an object.
FindControl only looks at the immediate children. You'd need to recursively navigate all the children, the children's children, etc., to find the control you're looking for.
public Control MyFindControl(Control parent, string controlIdToFind)
{
foreach(Control c in parent.Controls)
{
Control found = MyFindControl(c, controlIdToFind);
if (found != null)
{
return found;
}
}
// control not found.
return null;
}
Then change your code to:
Button logButton = (Button) MyFindControl(LoginView1, "LoginButton");
if (logButton != null)
{
this.Form.DefaultButton = logButton.UniqueID;
}
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.
I am trying to change the Visible state of controls from the form shown event.
I am reading the name of the controls from the database table and accessing it using this.Controls["controlname"].Visible. But some of the controls are not able to access from within this event. It is showing exception.
How can I access the controls from form shown event?
Use Controls.Find() to search for it. As scheien pointed out, the control is probably inside a different container causing it not to be "found" with your original syntax. Here's a quick example:
private void Form1_Shown(object sender, EventArgs e)
{
string ctlNameFromDatabase = "textBox1";
Control[] matches = this.Controls.Find(ctlNameFromDatabase, true);
if (matches.Length > 0)
{
// ... do something with "matches[0]" ...
// you may need to CAST to a specific type:
if (matches[0] is TextBox)
{
TextBox tb = matches[0] as TextBox;
tb.Text = "Hello!";
}
}
else
{
MessageBox.Show("Name: " + ctlNameFromDatabase, "Control Not Found!");
}
}
EDIT:
For MenuItems you'll have to flag the control name in the database as a "menu item" and then use this code, where menuStrip1 is the name of your MenuStrip, to find them:
string menuName = "copyToolStripMenuItem";
ToolStripItem[] matches = menuStrip1.Items.Find(menuName, true);
if (matches.Length > 0)
{
matches[0].Visible = true;
}
The same code will work for ToolStrips as well. For example, replace menuStrip1 with toolStrip1.
I'm creating a multi-tabbed .NET application that allows the user to dynamically add and remove tabs at runtime. When a new tab is added, a control is added to it (as a child), in which the contents can be edited (eg. a text box). The user can perform tasks on the currently visible text box using a toolbar/menu bar.
To better explain this, look at the picture below to see an example of what I want to accomplish. It's just a mock-up, so it doesn't actually work that way, but it shows what I want to get done. Essentially, like a multi-tabbed Notepad.
View the image here: http://picasion.com/pic15/324b466729e42a74b9632c1473355d3b.gif
Is this possible in .NET? I'm pretty sure it is, I'm just looking for a way that it can be implemented.
You could use a simple extension method:
public static void PasteIntoCurrentTab(this TabControl tabControl)
{
if (tabControl.SelectedTab == null)
{
// Could throw here.
return;
}
if (tabControl.SelectedTab.Controls.Count == 0)
{
// Could throw here.
return;
}
RichTextBox textBox = tabControl.SelectedTab.Controls[0] as RichTextBox;
if (textBox == null)
{
// Could throw here.
return;
}
textBox.Paste();
}
Usage:
myTabControl.PasteIntoCurrentTab();
I suggest you keep some "current state" variables updated so you always have a pointer to the selected Tab Page, and its child control (in the case of a tabbed-notepad emulation discussed here : a TextBox). My preference would be to keep track of the TabPage<>TextBox connections using a Dictionary to avoid having to cast the TextBoxes if they are accessed using the TabPage.Controls route : the following code assumes you have a TabControl named 'tabControl1 on a Form :
Dictionary<TabPage, TextBox> dct_TabPageToTextBox;
int tabCnt = 1;
TabPage currentTabPage;
TextBox currentTextBox;
So, as you create each new TabPage at run-time you call something like this :
private void AddNewTabPage()
{
if (dct_TabPageToTextBox == null) dct_TabPageToTextBox = new Dictionary<TabPage, TextBox>();
currentTabPage = new TabPage("Page " + tabCnt.ToString());
tabControl1.TabPages.Add(currentTabPage);
currentTextBox = new TextBox();
dct_TabPageToTextBox.Add(currentTabPage, currentTextBox);
currentTabPage.Controls.Add(currentTextBox);
currentTextBox.Dock = DockStyle.Fill;
currentTextBox.Text = "sample text for page " + tabCnt.ToString();
tabControl1.SelectedTab = currentTabPage;
tabCnt++;
}
As the end-user changes the selected TabPage you can simply update your current state variables like this :
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
currentTabPage = tabControl1.SelectedTab;
currentTextBox = dct_TabPageToTextBox[currentTabPage];
MessageBox.Show("text in current Tab Page is : " + currentTextBox.Text);
}
So now have the code that is invoked by your menu choices applied only to the currentTextBox.
best, Bill
I tried this for fun ... I made a form with a ToolStripContainer, and a ToolStrip inside it, with the standard buttons (which includes the paste button). I renamed the paste button to pasteButton, and hooking everything up you get:
public Form2()
{
InitializeComponent();
TabControl tc = new TabControl();
toolStripContainer1.ContentPanel.Controls.Add(tc);
tc.Dock = DockStyle.Fill;
TextBox selectedTextBox = null;
pasteButton.Click += (_, __) => selectedTextBox.Paste(Clipboard.GetText(TextDataFormat.Text));
int pages = 0;
newTabButton.Click += (_,__) => {
TextBox tb = new TextBox { Multiline = true, Dock = DockStyle.Fill, ScrollBars = ScrollBars.Vertical };
TabPage tp = new TabPage("Page " + (++pages).ToString());
tc.Selected += (o, e) => selectedTextBox = e.TabPage == tp ? tb: selectedTextBox;
tp.Controls.Add(tb);
tc.TabPages.Add(tp);
tc.SelectedTab = tp;
selectedTextBox = tb;
};
}
I'm trying to get all controls in a winform disabled at the Load event.
I have a form (MDI) which loads a Login Form. I want to disable the controls behind the Login Form to only let the user enter his username and password, and then if the user is valid re-enable the controls again.
Just show the login form as a modal dialog, i.e., frm.ShowDialog( ).
Or, if you really want to disable each control, use the Form's Controls collection:
void ChangeEnabled( bool enabled )
{
foreach ( Control c in this.Controls )
{
c.Enabled = enabled;
}
}
I suggest doing it this way instead of simply setting the Form's Enabled propery because if you disable the form itself you also disable the tool bar buttons. If that is ok with you then just set the form to disabled:
this.Enabled = false;
However, if you are going to do this you may as well just show the login prompt as a modal dialog :)
Simple Lambda Solution
form.Controls.Cast<Control>()
.ToList()
.ForEach(x=>x.Enabled = false);
Container like Panel control that contains other controls
then I used queue and recursive function get all controls.
for (Control control in GetAllControls(this.Controls))
{
control.Enabled = false;
}
public List<Control> GetAllControls(Control.ControlCollection containerControls, params Control[] excludeControlList)
{
List<Control> controlList = new List<Control>();
Queue<Control.ControlCollection> queue = new Queue<Control.ControlCollection>();
queue.Enqueue(containerControls);
while (queue.Count > 0)
{
Control.ControlCollection controls = queue.Dequeue();
if (controls == null || controls.Count == 0)
continue;
foreach (Control control in controls)
{
if (excludeControlList != null)
{
if (excludeControlList.SingleOrDefault(expControl => (control == expControl)) != null)
continue;
}
controlList.Add(control);
queue.Enqueue(control.Controls);
}
}
return controlList;
}
Just for some fun with linq, because you can.....
What you could do is create a "BatchExecute" extension method for IEnumerable and update all your controls in 1 hit.
public static class BatchExecuteExtension
{
public static void BatchExecute<T>(this IEnumerable<T> list, Action<T> action)
{
foreach (T obj in list)
{
action(obj);
}
}
}
Then in your code....
this.Controls.Cast<Control>().BatchExecute( c => c.enabled = false);
Cool.
I agree that ShowDialog is the way to go, but to answer the original question, you can do this if you want to disable all controls:
foreach (Control c in this.Controls)
{
c.Enabled = false;
}
As Ed said, showing the form as a modal dialog will do what you want. Be sure to check the dialog result returned from ShowDialog in case they cancel it instead of clicking login.
But if you really want to disable all the controls on the form then you should be able to just disable the form itself, or some other parent control like a panel that has all controls in it. That will disable all child controls. This will also allow the child controls to go back to their previous state when the parent control is enabled again.
Trying the ShowDialog show this exception:
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
What im doing is this:
private void frmControlPanel_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
ShowLogin();
//User = "GutierrezDev"; // Get user name.
//tssObject02.Text = User;
}
private void ShowLogin()
{
Login = new frmLogin
{
MdiParent = this,
Text = "Login",
MaximizeBox = false,
MinimizeBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog,
StartPosition = FormStartPosition.CenterScreen
};
Login.ShowDialog();
}