Refresh or add ascx control from another ascx control - c#

I am working on an asp .net project and i have a main ascx control which is divided by two RadSpliters. On load of the main.ascx the app is loading two other controls, the control1.ascx and control2.ascx. In the control1 i have a treeview and on selected node of the treeview i want to reload the control2.ascx. Is there a way to do this. Below i am pasting the code that i am using to do that but is not working. Any help or suggestions please?
public partial class Control1: System.Web.UI.UserControl
{
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
}

public interface IReload{
public void reload();
}
public partial class Control2: System.Web.UI.UserControl, IReload
{
}
public partial class Control1: System.Web.UI.UserControl
{
IReload _r;
public IReload setReload
{
set { _r = value; }
}
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
BrowseProject b = new BrowseProject();
b.load();
if(_r != null){
_r.reload();
}
}
public partial class MainControl : System.Web.UI.UserControl
{
public void load()
{
Control codeEditor = Page.LoadControl("Control2.ascx");
PlaceHolder4.Controls.Clear();
PlaceHolder4.ID = "PlaceHolder4";
PlaceHolder4.Controls.Add(codeEditor);
c.setReload(codeEditor);
}

Related

c# Get variable value from Form when value changed with timer in usercontrol

I have a usercontrol which have timer
public partial class Cumle : UserControl
{
private bool cond=false;
//Some Code....
private void timer2_Tick(object sender, EventArgs e)
{
//Some Code....
if(//some condition...)
cond=true;
}
}
I am working on windows form.I want to display a message box which shows me that cond is true.I want to make this stuff without using timer on Form.
public partial class Form1 : Form
{
//What I must write here?
}
As mentioned, you should use Events. I would go like this:
public partial class Cumle : UserControl
{
public event EventHandler ConditionChangedToTrue;
protected virtual void OnConditionChangedToTrue(EventArgs e)
{
if (ConditionChangedToTrue != null)
ConditionChangedToTrue(this, e != null ? e : EventArgs.Empty);
}
private void timer2_Tick(object sender, EventArgs e)
{
//Some Code....
if (true) // add your condition
{
cond = true;
OnConditionChangedToTrue(null);
}
}
}
public partial class Form1 : Form
{
private Cumle cumle = new Cumle();
public Form1()
{
InitializeComponent();
cumle.ConditionChangedToTrue+= Cumle_ConditionChangedToTrue;
}
private void Cumle_ConditionChangedToTrue(object sender, EventArgs e)
{
// add your event handling code here
throw new NotImplementedException();
}
}
You need to add a public event to your UserControl, and subscribe to it from your main form.
Something like this should do it:
public partial class Cumle : UserControl
{
public event Action<bool> ConditionChanged = delegate {};
private bool cond=false;
//Some Code....
private void timer2_Tick(object sender, EventArgs e)
{
//Some Code....
if(//some condition...)
{
cond=true;
ConditionChanged(cond);
}
}
}
Then in your form:
public partial class Form1 : Form
{
void SubscribeToConditionChanged()
{
myUserControl.ConditionChanged += ShowDlg;
}
void ShowDlg(bool condition)
{
MessageBox.Show("....");
}
}

Interaction between two user controls in windows form application

I created two user controls which called UserControl1 and UserControl2, UserControl1 contains TextBox1 and UserControl2 contains Button1. In the UserControl2, I want to get TextBox1.Text from UserControl1 when click Button1.
This is revelant code:
In UserControl1:
public partial class UserControlA: UserControl
{
public UserControlA()
{
InitializeComponent();
}
public string TexBoxText
{
get
{
return this.textBox1.Text;
}
}
}
In UserControl2:
public partial class UserControlB: UserControl
{
public UserControlB()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//here is to get textbox1.text
}
}
What should I do?
One option is passing UserControlA instance to UserControlB's constructor.
public partial class UserControlB: UserControl
{
UserControlA userControlA;
public UserControlB(UserControlA ucA)
{
userControlA = ucA;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string myString = userControlA.TexBoxText;
}
}
In UserControl1:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public TextBox TextBox
{
get
{
return textBox1;
}
}
}
In UserControl2:
public partial class UserControl2 : UserControl
{
private TextBox txt = null;
public UserControl2()
{
InitializeComponent();
}
public TextBox TextBox
{
set
{
txt = value;
}
}
private void button1_Click(object sender, EventArgs e)
{
if (txt != null)
MessageBox.Show(txt.Text);
}
}
In above controls' container:
uc2.TextBox = uc1.TextBox;

Modified UserControl

I have following control class withsome extra methods in it. I have multiple form usercontrols, FormChoosePage has dropdownlist with form list in it, and I get selected forms user control, load its data and after he click save button I call forms save method.
at btnSave_Click ucForm is null, I have 2 question;
1 ) how can I keep dynamicly Modified UserControl (View State or something?)
2 ) am I on right way to do this with generic user controls and everything. If not, what is you sugestion?
FormControler
public class Controler : UserControl
{
public virtual void PageLoad() { }
public virtual void SaveForm() { }
}
Form UC
public partial class ApplicationForm : Controls.Controler
{
public override void PageLoad()
{
ddlFormType.DataSource = [Data];
ddlFormType.DataBind();
}
public override void SaveForm()
{
XForm form = new XForm();
form.something = txtSomething.Text;
form.Save();
}
}
Form Choose Page
public partial class FormChoosePage: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ddlFormLoad();
}
}
Controler ucForm;
protected void ddlForm_SelectedIndexChanged(object sender, EventArgs e)
{
XForm form = XForm.Get<XForm>(ddlForm.SelectedValue);
ucForm = this.LoadControl(form.URL) as Controler;
ucForm.ID = "ucForm";
ucForm.PageLoad();
}
protected void btnSave_Click(object sender, EventArgs e)
{
ucForm.SaveForm();
}
}

How to disable/enable a TextBox inside a UserControl from my WebForm

I created a UserControl called "DadosFuncionario". Now, I'm using it in my WebForm. Sometimes, I need to enable or disable controls of my UserControl. It depends if I'm viewing or editing my record.
I tried to do it in two ways but no one of them is working.
Those are what I have tried:
1º
In my web form:
public partial class MyWebForm : System.Web.UI.Page
{
private void EnableFields()
{
MyUserControl.EnableFields();
}
private void DisableFields()
{
MyUserControl.DisableFields();
}
}
public partial class MyUserControl : System.Web.UI.UserControl
{
public void EnableFields()
{
txtName.Enabled = true;
}
public void DisableFields()
{
txtName.Enabled = false;
}
}
2º
/***************************************/
In my Web Form
public partial class MyWebForm : System.Web.UI.Page
{
private void EnableFields()
{
((TextBox)MyUserControl.FindControl("txtName")).Enabled = true;
}
private void DisableFields()
{
((TextBox)MyUserControl.FindControl("txtName")).Enabled = false;
}
}
I thought that maybe my UpdatePanel was interfering but I added a trigger referencing my UserControle and anything happened.
Any idea about what to do to solve this issue?
Best Regards.

Subscribing to an event of a ascx of another ascx in a different window

I'm trying to subscribe to the the save button event of a user control that is launched in a separate radwindow from the calling parent. but I am getting object not initialized error, I know why but what am I missing?
Update: I found my error but it appears that if (this.SaveEvent!= null) in the ControlBase is always null
Parent Control Code:
public partial class myControl : ControlBase
{
private myChildControl __myChildControl;
private void myControl_PreRender(object sender, EventArgs e)
{
// error occurs here
//this.__myChildControl.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
// found my error
this.SaveEvent += new myChildControl.SaveEventHandler(__myChildControl_SaveEvent);
}
private void __myChildControl _SaveEvent(object sender, CustomEventArgs e)
{
this.Label1.Text = e.CustomEventArg1.ToString();
this.Label2.Text = e.CustomEventArg2.ToString();
}
}
Child Control Launched in RadWindow:
public partial class myChildControl : ControlBase
{
protected void btnSave_OnClick(object sender, EventArgs e)
{
CustomEventArgs _cea = new CustomEventArgs {CustomEventArg1 = 123, CustomEventArg2 = 12};
callBaseMethod(_cea);
}
}
ControlBase Code:
public class ControlBase : UserControl
{
public event CustomEventHandler SaveEvent;
public delegate void CustomEventHandler(object sender, CustomEventArgs e);
internal void callBaseMethod(CustomEventArgs cea)
{
if (this.SaveEvent!= null)
{
this.SaveEvent(this, cea);
}
}
}
CustomEventArgs class:
public class CustomEventArgs : EventArgs
{
public int CustomEventArgs1 { get; set; }
public int CustomEventArgs2 { get; set; }
}
This isn't possible in codebehind: the RadWindow presents a separate aspx/ascx page altogether that is linked to the main page through javascript alone.
What you need to do is handle the RadWindow OnClientClose event in javascript, then fire something in the parent page that performs the appropriate tasks.

Categories

Resources