Custom Control Property Reference Another Control - asp.net

I have been trying to create a property in a custom web control (DropDownMenu) that references another control (Button) on the page.
This is my property declaration (inside DropDownMenu):
<Bindable(True), Category("Behaviour"), Localizable(True)>
Private _ToggleParent As Control
Public Property ToggleParent As Control
Get
Return _ToggleParent
End Get
Set(value As Control)
_ToggleParent = value
End Set
End Property
And this is my mark-up:
<boot:Button runat="server" ID="t1" Text="hi" />
<boot:DropDownMenu runat="server" ToggleParent="t1" >
<MenuItems>
<boot:MenuItem runat="server" Text="test"></boot:MenuItem>
<boot:MenuItem runat="server" Text="test"></boot:MenuItem>
</MenuItems>
</boot:DropDownMenu>
This is what I get as the error:
Cannot create an object of type 'System.Web.UI.Control' from its string representation 't1' for the 'ToggleParent' property.
Same error if I remove the quotes. I understand I could FindControl based on a string representation of the control ID, but I would prefer to pass the control directly through the property.

Related

how to access to properity in usercontrol from html

i have public property in user control and i need access to this property from HTML for parent for this user control
Public Property ActorID As Integer
Set(value As Integer)
ViewState("ActorID") = value
End Set
Get
Return ViewState("ActorID")
End Get
End Property
<uc3:Hos_Applicants ID="Hos_Applicants1" runat="server" />
<uc3:Hos_Applicants ID="Hos_Applicants1" runat="server" ActorID="2" />
you can access to property and set value in user control from parent asp page

Client-side validation for two separate conditions

Is there a way to validate asp.net text box for input using built-in validators for two distinct conditions? The text box is used in conjunction with a date picker and presently I have RegularExpressionValidator like this to validate:
<asp:TextBox ID="tbStartDate" runat="server" Width="100" CssClass="TextCompact">yyyy</asp:TextBox>
<asp:HyperLink ID="hlStartDate" runat="server" ImageUrl="~/Images/iconCalendar.gif"></asp:HyperLink><br />
<asp:RegularExpressionValidator ID="revStartDate" ControlToValidate="tbStartDate"
CssClass="TextCompact" runat="server" ErrorMessage="* Invalid Date" ValidationExpression="^(\d{4})$" />
which fires everytime an invalid date is entered - you can use the date picker or you can type in a date string.
So the issue is that I only want to validate if there is data in the text box. If the text box is empty I want it to pass through without validation.
Is this possible or would a custom client side validation be required?
Thanks,
Risho.
Update:
I've decided to write my own validator class derived from the BaseValidator class and wired it up but I get a compiler error.
namespace Abcdefg1234
{
public class Custom_Validator : BaseValidator
{
public Custom_Validator()
{
}
protected override bool EvaluateIsValid()
{
String value = this.GetControlValidationValue(this.ControlToValidate);
return ValidateControls(value.Trim());
}
public static bool ValidateControls(string value)
{
if(Regex.IsMatch(value, #"^(\d{4})$") == true)
return true;
else if(value == "")
return true;
else
return false;
}
}
}
Then I included the page directive tag:
<%# Register TagPrefix="Cv" Namespace="Abcdefg1234" TagName="CustomValidaitonX" %>
and the actual tag:
Cv:Custom_Validator ID="cvtbStartDate" ControlToValidate="tbStartDate" runat="server" ErrorMessage="* Invalid Date" />
I get a unknown server tag. Any ideas? Thanks.
The only built-in validator that fires on an empty text box would be the RequiredFieldValidator. If a textbox is empty, the other validators associated with it won't fire on form submission.
In other words, you should be fine just as you are, with the RegularExpressionValidator not firing if you submit with the textbox still empty. Are you experiencing different behavior?
ETA: You need to specify the Assembly name in your Register page directive for the Custom_Validator to be recognized. I don't believe the tag name is required for a custom control.

Hyperlink inside dynamic userControl

I have a userControl being dynamically added to my form, anywhere from 1 to 20 times.
The control contains hyperlink field (visible) and a reference field (invisible). The problem is that the hyperlink is not passing the value of the reference field as linked page parameter.
The userControl coding looks like this:
<asp:Hyperlink ID="childLink" Enabled="true" DataNavigateUrlFields="aSeq"
DataNavigateUrlFormatString="~/Header.aspx?aSeq={0}" NavigateUrl="~/Header.aspx"
runat="server><%# DataBinder.Eval(Container, "DataItem.cName") %> </asp:HyperLink>
So, the hyperlink should open the Header page, passing the value of the "aSeq" field which looks like this:
<asp:HyperLink ID="aSeq" Enabled="true" DataNavigateUrlFields="aSeq" DataNavigateUrlFormatString="~/Header.aspx?aSeq={0}" NavigateUrl="~/Header.aspx"
runat="server"><%# DataBinder.Eval(Container,"DataItem.genePK") %></asp:HyperLink>
It does go to the page load for Header.aspx page, but the Request.QueryString["aSeq"] is always NULL. On the Page_Load I have a short block of code:
string aRequest = Request.QueryString["aSeq"];
if (aRequest==null)
{
PopulateHeader("GKEAAHDI");
}
else
{
PopulateHeader(aRequest);
}
I can only surmise it has not gotten the value of the field tagged "aSeq". I used a hyperlink because a textbox will not allow a code block for the databinder.
What have I done!?
According to MSDN " The value of the field specified by the DataTextField property is passed as a query string to a Web page specified in the format string." So you would need to add DataTextField="aSeq" in your asp:hyperlink declaration.
Not tested, just researched.

Using codeblocks within usercontrols

I tried using a codeblock syntax within a property sent to a web user control:
<uc1:MyControl ID="MyControl1" runat="server" SomeProperty="<%= somevalue %>"/>
The user control has the public property SomeProperty declared and also uses code block to display the property value:
<p><% = SomeProperty %></p>
The output on my page is unfortunately
<p><%= somevalue %></p>
And not the actual value. Anyone know of some workaround for this?
You are trying to assign a server side value on a server side control - this is not possible.
You can use code blocks in client side code (that doesn't have a runat="server" attribute), this of course doesn't not apply to server side controls.
Set the attribute in the code behind (ascx), before OnRender:
// In onload, pre render or other event handler
MyControl1.SomeProperty = somevalue; // C#
MyControl1.SomeProperty = somevalue ' VB.NET
Try assigning the value of the property to a Label and call the .DataBind() method on the control.

asp.net usercontrol SetFocusOnError

I have a aspx page that several of the same usercontrols on the page. The usercontrol houses a textbox that has a Required field validator on it. The validator works but the setonfocus="true" does not seem to be working, further more, the button the aspx page when the validator shows the error message, the button still fires the code behind.
Here is what the aspx page looks like as far as the user control and the the button.
ucTB:ucTextBox ID="ucTextR" runat="server" ValidationGroup="txtRequired" Required="_true"
asp:Button ID="btnSave" runat="server" Text="Click" ValidationGroup="txtRequired"
and the usercontrol validator
asp:RequiredFieldValidator ID="rfTextBox" runat="server" ControlToValidate="txtTextBox"
SetFocusOnError="true" ErrorMessage="Required Field" EnableClientScript="false"
the user control has been wired to grab the validator from the aspx page and use it in the usercontrol... something like this
Public Property ValidationGroup() As String
Get
Return CType(ViewState("ValidationGroup"), String)
End Get
Set(ByVal Value As String)
ViewState("ValidationGroup") = Value
End Set
End Property
Protected Sub AssignValidation()
For Each control As Control In Me.Controls
Dim [property] As PropertyInfo = control.[GetType]().GetProperty("ValidationGroup")
If [property] Is Nothing Then
Continue For
End If
[property].SetValue(control, ValidationGroup, Nothing)
Next
End Sub
and i load the AssignValidation on page_load
anyway.. hope this is the info you need to point me in the right direction.
What i'm looking to do is if the required field validator to put the focus on the usercontrol if there is nothing in the usercontrol text box and also for the button on the aspx page not to fire.. like i think it behaves if you use a validator on a aspx page with no usercontrol
thanks
shannon
You can't set the user control to visible because it's not a visible container. You can set the focus yourself programmatically. See this:
http://forums.digitalpoint.com/showthread.php?t=282224
Or, you can programmably set the validator to the ID of the textbox within the user control; expose a textboxID property in the user control code-behind which returns the textbox's ID, and have your page assign the validator's controltovalidateID to this.

Resources