Using Control Adapter for User Controls in ASP.NET - asp.net

We can change the output rendering of an ASP.NET control with Control Adapters. Now I want to know that, is it possible to change a user control rendering by control adapters in ASP.NET? By the way I have several user controls in my page and I don’t want to do same actions for them.

Yes you can but you must define your controls childs as public in your User control
(Associate public on child controls in order to access from your adapter)
public class UserControlSample : userControl
{
public TextBox TextBoxWrapper
{
get
{
return MyTextBox;
}
set
{
MyTextBox = value;
}
}
}
//Access from adapter
UserControlSample.TextBoxWrapper.Text
UserControlSample.TextBoxWrapper.Id
.....

Related

ASP.net Server Controls and using AjaxcontrolToolkit

I have a question about server controls VS. user controls (.ascx). Currently, when I write a user control, I want to be able to take advantage of using the AjaxControlToolKit DLL. In particular, I want my user control to be able to do partial post backs and use the various extenders (modalPopUp for example). My boss, though, prefers us to use server controls so that we can compile them into a DLL and use them in various applications. My question is: If I rewrite my user controls to be server controls, will I still be able to use the AjaxControlToolKit and all its features (asyn post backs and extenders)?
FYI: I am using Visual Studio 2010, AjaxControlToolkit 4.1.60919 and .Net 4.0
Yes, you'll be able to do this. Just inherit your control from the CompositeControl class interface and add any extender or control from the ACT project to the Controls collection the same way as you should do this with ordinal ASP.NET server controls. Also you can inherit existing control and implement INamingContainer interface but in this case you must call RenderChildren method manually from control's Render method:
[DefaultProperty("Text")]
[ToolboxData("<{0}:WatermarkedTextBox runat=server></{0}:WatermarkedTextBox>")]
public class WatermarkedTextBox : TextBox, INamingContainer
{
private AjaxControlToolkit.TextBoxWatermarkExtender _watermarkExtender;
public string WatermarkText
{
get
{
return ViewState["WatermarkText"] as string;
}
set
{
ViewState["WatermarkText"] = value;
}
}
protected override void CreateChildControls()
{
Controls.Clear();
this._watermarkExtender = new AjaxControlToolkit.TextBoxWatermarkExtender
{
ID = "wte",
TargetControlID = this.ID,
WatermarkText = this.WatermarkText
};
this.Controls.Add(_watermarkExtender);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
RenderChildren(writer);
}
}
}

How to access attributes of inner tags in custom controls in asp.net

I have a custom control for building forms to automatically generate a label-input field pair. But I need to be able to modify attributes of the generated label-input field pair in my vb code behind file. The problem is that the code behind file seems to be applied before the custom control. How do I work around this issue?
You could expose the inner controls as public properties of your custom control.
For example:
public class MyCustomControl : CustomControl
{
Label _label;
// initialize _label in OnInit() ...
public string LabelText { get { return _label.Text; } set { _label.Text = value; } }
}

integrate custom control controls validation with Page.IsValid

I have created a custom server control. So far this control renders some html in the webpage. On submit of the page i need to take the values entered in the textbox of the server control and call some webservice to validate the input of the user. i don't want to write this code in code behind of the page that this control is used in. I want all the validations to be written in the server control itself and if validation fails, Page.IsValid should be set to false. If the user input value in server control is valid Page.IsValid will be true.
I am trying to achieve is the same functionality as google recaptcha. All user needs to do to use this control is to user the control in the page. user entered value is correct or incorrect is handled in the control itself and in the code behind of the page, there is only Page.IsValid. Here is the page on google that explains this
http://code.google.com/apis/recaptcha/docs/aspnet.html
and i have also used the google recaptcha and it works as expected. I also want to build same kind of functionality for my server control, Please help, if it is possible.
Thank's for answering the questions. I found the solution. Here is the entire code of the server control. The trick was to implement IValidator. It gives us two property and one metod. ErrorMessage and IsValid properties and Validate method. I wrote all the validation code in Validate method and set this.IsValid. This solved the problem.
[ToolboxData("<{0}:MyControl runat=server></{0}:MyControl>")]
public class MyControl : WebControl, IValidator
{
protected override void RenderContents(HtmlTextWriter output)
{
//Render the required html
}
protected override void Render(HtmlTextWriter writer)
{
this.RenderContents(writer);
}
protected override void OnInit(EventArgs e)
{
Page.Validators.Add(this);
base.OnInit(e);
}
public string ErrorMessage
{
get;
set;
}
public bool IsValid
{
get;
set;
}
public void Validate()
{
string code = Context.Request["txtCode"];
this.IsValid = Validate(code);//this method calls the webservice and returns true or false
if (!this.IsValid)
{
ErrorMessage = "Invalid Code";
}
}
}
You could incorporate the validator in the server control. It would need a server validate method to call the web service.
The net result would be a server control that you drop on the page, no other validators needed. if your control can't validate it's contents, then page.isvalid will be false.
Simon

User Control In Asp.net

I created a user control. It contains one textbox. can I control this from parent web page.
Add the following property to your user control:
public string SomeValue
{
get
{
return txtSample.Text;
}
set
{
txtSample.Text = value;
}
}
And if you want to get or set the user control's textbox value from the page that contains the user control, just do the following:
MyUserControl.SomeValue = "Hello from page";
lblTest.Text = MyUserControl.SomeValue;
Where "MyUserControl" is the ID of the user control in the containing page.
Note: Since the TextBox control handles its Text property in the ViewState on its own, you don't have to explicitly handle it for this property.
Just define some public method or property in the user control and you can access it successfully. For example:
In UserControl1.ascx.cs:
public void DoSomething()
{
//Do something here from UserControl
}
In the parent:
MyInstanceOfUserControl1.DoSomething();

how to get instance of .net control which is in child page from user control in master page

i have user control called shopping cart.
which i have used in master page.
i want to get the textbox value from the child page in to user control.
is it possible to access control from child page in "Custom User Control" which is on master page?
I'm not entirely sure what your trying to accomplish but it sounds like to want to access a property of a user control contained in a master page from a content page.
You can use a public property in your master page which exposes the text property of the user control.
public string ShoppingCartText {
get { return ((TextBox)this.ShoppingCart.FindControl("TextBox1")).Text; }
set { ((TextBox)this.ShoppingCart.FindControl("TextBox1")).Text = value; }
}
Then from your content page you can set the value of the text box. You can access the properties of a master page from a content page through the Page.Master property.
Master.ShoppingCartText = "value"
You can recurse through the control tree to find any control in a page.
Here are a couple of extension methods, pop this code in a class file in your solution.
public static class ControlExtensions
{
public static IEnumerable<Control> FindAllControls(this Control control)
{
yield return control;
foreach (Control child in control.Controls)
foreach (Control all in child.FindAllControls())
yield return all;
}
public static Control FindControlRecursive(this Control control, string id)
{
var controls = from c in control.FindAllControls()
where c.ID == id
select c;
if (controls.Count() == 1)
return controls.First();
return null;
}
}
Then use like this in your user control.
TextBox whatYoureLookingFor = this.FindControlRecursive("theId") as TextBox;
if(null != whatYoureLookingFor)
// whatever
What I did was access the master page control through a public function in the code behind.
So in the code behind for the master page, I would declare something like:
public string getTextBoxValue()
{
return TextBox.Text;
}

Resources