Parent user control validation from child user control ASP.Net - asp.net

I have a user control which contains two text fields each assigned with requiredfield validators this control contains another user control contains say a button .On click of this button i need to validate the fields from the parent control text fields.
I try the Page.Validate("ValidationGroup") with Page.IsValid it validates but the error message is not shown .Error message is shown only if i try to validate it from the contains which contains the required field validators ?

I ran into the same issue just now.
I solved it by adding a customvalidator beneath my reference to the usercontrol and validated the usercontrol from the parent by exposing the properties that required validation. I was exposing these properties anyway so no big deal there.
<div>
<uc:MyChildUserControl ID="MyChildUserControl1" runat="server"></uc:MyChildUserControl >
<asp:CustomValidator ID="MyChildUserControlCustomValidator" runat="server" ValidationGroup="default_validation" ErrorMessage="errormessage to show when the sh*t hit the fan" Text="*"></asp:CustomValidator>
</div>
And then the servervalidate code:
protected void MyChildUserControlCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
{
args.IsValid = MyChildUserControl1.SomeProperty;
}

And you have the ValidationGroup property set on all the buttons and TextBox's involved to the same validation group name? I've used the validationgroups in a few projects that have fairly deeply neested User controls and custom controls and as long as
ValidationGroup="CommonName" is set on the button being clicked and all the fields involved then the validation shows up properly..

Related

ASP .Net Textbox Textchanged event

I have a webpage. I show records from table, lets say, students in my page. I query all the students and show them in grid. I want to use a textbox for filtering the datagridview results. For example if the user types a in the textbox the grid will show only the students who has "a" in his/her name. I want to refresh the grid at the same time while textbox is being edited.
i have set the autopostback property of textbox to true, and i refresh the grid in textbox's textchanged event.But the textchanged event fires only after the textbox loses focus. How can I make it fire after user types just one character ? thanks.
You have to use the onKeyDown event. However, I'd advise you to use ASP.NET AJAX or jQuery to load the results with Ajax.
Here is one example from asp.net: http://www.asp.net/ajaxlibrary/AjaxControlToolkitSampleSite/AutoComplete/AutoComplete.aspx
Another one, from Code project:
http://www.codeproject.com/Articles/38803/Google-Like-Search-TextBox
You might want to show some your present code, if there is a particular method you want to go with for this. Otherwise your going to get a people telling you the way they would do it.
Does it look something like this right now?
<asp:Textbox id="myTextbox" runat="server" onChange="txtChanged" AutoPostBack="true"/>
public void txtChanged(object sender, EventArgs e)
{
//Get text from textbox
string text = ((TextBox)sender).Text;
//Do what ever it is you want to do to edit the text
text = text.ToUpper();
//Update the other textbox with this text
txtMyText2.Text = text;
}
I think the best and most clean way is to use Rad Controls, here is an example on how to do it:
http://demos.telerik.com/aspnet-ajax/controls/examples/integration/gridandcombo/defaultcs.aspx?product=grid
The event TextChanged only fires when you send a request to server. If you wanna launch an event or make a function when the text inside textbox changes, use an OnKeyDown event (right with Schiavini).
You can use PicNet to do this in the Client instead of the Server for a better User experience. You can find it here http://www.picnet.com.au/resources/tablefilter/demo.htm Remember that the Gridview is rendered as a HTML table, therefore you can freely use this jQuery plugin.
Good luck!

Hide Checkbox on a particular page

I am working on ASP.NET application where I am reusing a user control. The user control contains a checkbox and bunch of other controls. I want to display all the controls inside the user control on all the pages but on one single page I want to hide the checkbox.
I was thinking that I can use the databind methods and see if I am on the "pagex" then hide the checkbox. Is there any other way to solve this problem?
If you have access to the code for the control you should be add a new property to the control for hiding/showing the checkbox and then just pass in the property depending upon what page you are on. You'd have to pass in the show/hide property on the load event of the page.
Do you have access to the code??
I would specify a parameter in the code behind for your ascx file. Example:
public bool HideCB = false;
THen when you put your User Control on your aspx page do this:
<uc:TestControl id="TestControl" runat="Server" HideCB="true" />
This way you can do a check on if(HideCB) to determine if you want to make it visible or not.

Repeater Item Command Causes Validation

I seem to have a bit of a bug, I have a ASP.NET repeater control with a link buttons in it and the link button has the have the causes validation property set to false.
However; when clicking it which makes a panel visible on the web page, the asp.net required field validator controls trigger and shows their error messages. On those controls that I have the validator controls on.
Any ideas as to what might cause it to be ignoring the causes validation property set to false?
On my opinion, you should set different ValidationGroup properties values for repeater control and for control that is the source for required field validator. It is possible that container for repeat control has raised event that can be heared by required field validator.
If mentioned above cannot help then try to disable client validation for RequiredFieldValidator using EnableClientScript="False" for it. And activate RequiredFieldValidator when it really usefull. For example in the some button event handler you can apply such code:
MyButton.Validate();
if (MyButton.IsValid)
{
Do what you want...
}
For anybody that has this problem and stumbles across this post, here's what I found.
Turns out the problem was happening because I had EnableViewState="false" set on the Repeater. This was breaking the event postback somehow, and making every validator on the page fire. All I had to do was manually call DataBind() on the Repeater from within Page_Load(), and it cleared right up.
try to set the visablity of the panel true all the time in design view,, and check the validation again.

Validation is not working

I have one asp content page.Its contain many controls like dropdownlist,textbox etc.All controls are inside a div tag.I gave required field validator for all my drop down list.i have one SAVE button that reside inside another div tag.I gave SAVE button cause validation true.But my problem is that, the validator is not working and the page.Isvalid property is true.What is the problem with my code?
Please make sure you have set the ControlToValidate property to the DropDownList id. Also you need to set the InitialValue property on the RequiredFieldValidator when validating a DropDownList. This basically just tells the Validator which item is the intial value (which will throw a validation error when it is selected).
Hope this helps,
Neil

How can I set the ValidationGroup dynamically

I have a ASP.NET 2.0 webpage with 2 UserControls (.ascx). Each UserControl contains a bunch of validators. Placing a ValidationSummary on the page will display all validation errors, of both UserControl's. Placing a ValidationSummary in each UserControl will display all the errors of both controls twice.
What I want is a ValidationSummary for each UserControl, displaying only the errors on that UserControl.
I've tried to solve this by setting the ValidationGroup property of the validators on each usercontrol dynamicaly. That way each validationsummary should display only the errors of its UserControl. I've used this code:
foreach (Control ctrl in this.Controls)
{
if (ctrl is BaseValidator)
{
(ctrl as BaseValidator).ValidationGroup = this.ClientID;
}
}
ValidationSummary1.ValidationGroup = this.ClientID;
This however seems to disable both clientside and server side validation, because no validation occurs when submitting the form.
Help?
The control that is causing your form submission (i.e. a Button control) has to be a part of the same validation group as any ValidationSummary and *Validator controls.
If you use ValidationGroups, the validation only occurs if the control causing the postback is assign to the same ValidationGroup.
If you want to use a single control to postback you can still do this but you would need to explicitly call the Page.Validate method.
Page.Validate(MyValidationGroup1);
Page.Validate(MyValidationGroup2);
if(Page.IsValid)
{
//do stuff
}
Suggestion:
Why don't you expose a public property on your user controls called ValidationGroup?
In the setter you could explicitly set the validation group for each validator. You could also use your loop, but it would be more efficient to set each validator explicitly. This might improve the readability of the code using the user controls.

Resources