Wednesday, March 3, 2010

To access control of a User Control from an aspx page

9:44 AM Posted by Unknown No comments
Whenever you want to access the controls of an usercontrol, create public properties for those controls in your usercontrols and you could easily access them in your .aspx page.

Here it goes how...
UserControl.

1 public partial class WebUserControl : System.Web.UI.UserControl
2 {
3 public string TextBoxValue {
4 get { return TextBox1.Text; }
5 set { TextBox1.Text = value; }
6 }
7 protected void Page_Load(object sender, EventArgs e) {
8
9 }
10 }

In your .aspx page you can access it either html or code
<%@ Register TagPrefix="DS" TagName="TB" Src="~/WebUserControl.ascx" %>




or

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
myTB1.TextBoxValue = "Sample Text";
}
}

Friday, February 26, 2010

Monday, February 15, 2010

Thursday, February 4, 2010

Wednesday, February 3, 2010

Calling method of a usercontrol from another usercontrol

10:21 AM Posted by Unknown 1 comment
I need to call the method in a usercontrol from another usercontrol. Both controls are placed in same page.
If it is a web application, both controls will be in same namespace. So, we can get their names in the codebehind.

for eg.
if control1 is to be found from control2.
both control1 and control2 are in folder controls. webapplication name is test. So the namespace would be test.control2. Since, both these controls are under the same namespace, we can find the control2 in control1 and vice versa directly.
control2 2 = control1.FindControl("control21") as control2;

But, if this is a website, no namespace. So, we have to add a reference.
Add reference in control1 to find control2.
<%@ Reference Control="~/controls/control2" %>
then, in codebehind,
ASP.controls_control2 2 = this.FindControl("control21") as ASP.controls_control2;

Finding Controls

10:17 AM Posted by Unknown No comments
Check the name of the control in the rendered html. For eg.
I need to find gridview "grdwSelectedCandidates" in usercontrol "SelectedCandidates.ascx" which is inside the ajax tabpanel inside ajax tabcontainer.
I need to find this from another control which is placed in another tabpanel in the same container.
The rendered name is "ctl00_ContentPlaceHolder1_TabContainer1_TabPanel6_SelectedCandidates1_grdwSelectedCandidates"
So, I have to first find content place holder, then, tabcontainer, then, tabpanel, then, control and then gridview.