add custom web user control in onclick event handler function - c#

I have an asp.net project and I have a panel in it in which I want to add multiple user control on the panel by clicking on the button, It does this when clicking on the button
System.Web.UI.Control _msg = (System.Web.UI.Control)Page.LoadControl("msgsend.ascx");
pnl.control.add(_msg)
but it clears all the user controls on the panel before and adds only one user control. What should I do to avoid this?

Related

Devexpress unload usercontrol after save form

i have a main form which is containing a menu.
when i click a menu button there is an usercontrol is loading panel control in main form:
splitContainerControl1.Panel2.Controls.Add(new Moduller.userControlStokListesi() {
Dock = DockStyle.Fill
});
And there is a form on UserControl. when a user fill that form and hit the save button i want to remove that user control form from panel control.
How can i do that?
So if I understand your question correct, you want to remove the instance of Moduller.userControlStokListesi from the Panel2 ?
There are several ways to achieve this. You can remove all controls from a panel this way:
splitContainerControl1.Panel2.Controls.Clear();
You can also remove specific items:
splitContainerControl1.Panel2.Controls.RemoveByKey("the key of your control");
Or if you want the user control removes itself from the panel, you can call this snippet within the user control instance:
SplitContainerControl splitPanel = (SplitContainerControl) this.Parent;
splitPanel.Panel2.Controls.Remove(this);

How to caputure page level events on a user control on that same page

Is it posible to caputure button click event that is on a page, to a User Control which is with in that page.
If you are trying to pass a parent's button click event to a child, you can do it.
In the User Control, make a method called HandleParentButtonClick(passing necessary parameters). In the parent's OnClick event, call the User Control's HandleParentButtonClick method.

How to validate WebUser Control inside Page

I have a web user control inside my page. In the web user control I have a radio button list and a required field validator to check whether any of the radio buttons are clicked or not. However, on the main page I have a button which, when clicked, should check the validation. I don't know how to do that.
Here is the structure:
Page -> usercontrol -> radiobuttonList
|
|-> button
When the button is clicked, I would like the web user control radiobuttonlist validator to perform its validations and set Page.IsValid accordingly.
If you set the validationgroup property on all your controls and button to the same string it won't matter if the validators are in separate controls or all on the one web form.
I.E validationgroup="MyFormToValidate"
Here is the documentation on that property:
http://msdn.microsoft.com/en-us/library/ms227424(v=vs.100).aspx
You can use a CustomValidator Control and specify the OnServerValidate method.
protected void cvRBLCheck_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = radiobutton.Checked;
}
On your button, ensure that CausesValidation is set to true.
When your button is clicked, the OnServerValidate method will fire and you can do your checks in there.

get the splitcontainer context in user control click event

Sir,
I have a split container in which in right panel i have a usercontrol.In the user control i have few buttons like view,new,edit etc.Bottom of that user control a form will open based on what link is clicked on the left side navigation pane. now when i click the user control's view button, i should open a new form below it. how to get the context of splitcontainer in the click event?also if i want to retrieve the form values to save in database when i click the save button in user control, how to do it?
A Click event has a sender parameter, which is the clicked button. You could use the name of the button to resolve which form should be opened.
To get your button in the click event:
Button clickedButton = (Button)sender;
To get the parent of your button (if it was SplitContainer, you'll have to use Parent property 3 times, because the first one will get you your UserControl, second - left panel of the SplitPanel, which doesn't have a Name property, third - your SplitPanel, and 4th, if you want, your form name)
string splitPanemName = clickedButton.Parent.Parent.Parent.Name;
...or you can just get whole SplitPanel object:
SplitPanel currentSplitPanel = (SplitPanel)clickedButton.Parent.Parent.Parent;
...or Form object:
Form currentSplitForm = (Form)clickedButton.Parent.Parent.Parent.Parent;
To do this, you have to be sure of the composition of your Form, so you can get the right controls at the right place.

User Control Click - Windows Forms

I have a custom user control on my windows forms. This control has a few labels on it.
I will be dynamically displaying an array of these controls on my form which will contain different bits of data.
What I am trying to do is know which user control was selected when I click on it.
This works when I click on an empty space on the user control, however, if I click on any label on the user control it will not recognize the user control click.
Any thoughts on how I can do a full user control click, even if a label on the control is being clicked?
If this question is not clear, or you need more info, please leave a comment.
I am doing this in c#.
Thanks!
User control's click event won't fire when another control is clicked on the user control. You need to manually bind each element's click event. You can do this with a simple loop on the user control's codebehind:
foreach (Control control in Controls)
{
// I am assuming MyUserControl_Click handles the click event of the user control.
control.Click += MyUserControl_Click;
}
After this piece of code workd, MyUserControl_Click will fire when any control on the user control is clicked.
foreach (Control c in this.Controls)
{
c.Click += new EventHandler(SameAsForm_Click);
}
Keep in mind that this won't add labels' clickevents in groupboxes, panels etc to the "SameAsForm_Click"-EventHandler.

Categories