AJAX :: Created A Custom Validation Control That Compares Two Textboxes And Verifies Their Content?
Jul 19, 2010
I recently created a custom validation control that compares two textboxes and verifies their content. When I use this control in a detailsview I have no problems but, when an updatepanel is added the validation control no longer works.I'm using Visual Studio 2010 and .net 3.5 framework.
I have created some dynamic textboxes with standard content.Does anyone know how can I read the content of these textboxes (assuming that user modified the standard content) when I press one button?This is how I am creating the textboxes:
foreach (string name in listOfNames) { TextBox tb = new TextBox(); tb.Text = name; tb.BorderStyle = BorderStyle.None; tb.BorderWidth = 0; tb.Font.Name = "Arial"; tb.Font.Size = 8; }
I have a custom control that is based off the gridview control located at: here The control is basically a gridview that automatically creates a column of checkboxes that can be used to "check" individual rows in the gridview. During the gridview's "CreateColumns" event, the "checkboxcolumn" is created dynamically. The checkboxcolumn also contains another checkbox in the header that is used to "select/deselect all" checkboxes in the column. Since the gridview does not automatically remember the state of the checkboxes in the checkboxcolumn on postback, I added a method to the control called "SaveCheckBoxState" which stores the indexes of the checked rows in Viewstate, and then I modified the "OnRowDataBound" event to check the Viewstate and reset the checkboxes based on the Viewstate.
I then added a call to "SaveCheckBoxState" in the gridview's OnSorting and OnPageIndexChanging events. This works great so long as I'm sorting or changing pages. However, I need it to update the viewstate everytime someone clicks or unclicks one of the checkboxes. At this time, the checkboxes are rendered with an onclick event that calls some javascript to highlight the row, or in the case of the checkbox in the header, to select/deselect all checkboxes. I need to call the "SaveCheckBoxState" method from the javascript used by the customcontrol, or I need to find a way to modify viewstate from javascript and perform the same action as "SaveCheckBoxState".
I've tried adding the "SaveCheckBoxState" to the onclick event declaration in the checkboxes, but when run, it simply tells me that the method is undefined. It doesn't exist in the parent page, and I don't think I should have to make an event for the parent page to pass the click to. It seems to me this should be all self contained within the custom control. Does anyone know how I can acheive this? Here is the code for the gridview OnPreRender event where the onclick event of the checkbox is set:
protected override void OnPreRender(EventArgs e) { // Do as usual base.OnPreRender(e); // Adjust each data row foreach (GridViewRow r in Rows) { // Get the appropriate style object for the row TableItemStyle style = GetRowStyleFromState(r.RowState); // Retrieve the reference to the checkbox CheckBox cb = (CheckBox)r.FindControl(InputCheckBoxField.CheckBoxID); // Build the ID of the checkbox in the header string headerCheckBoxID = String.Format(CheckBoxColumHeaderID, ClientID); // Add script code to enable selection cb.Attributes["onclick"] = String.Format("ApplyStyle(this, '{0}', '{1}', '{2}')", SelectedRowStyle.CssClass, style.CssClass, headerCheckBoxID); // Update the style of the checkbox if checked if (cb.Checked) { r.BackColor = SelectedRowStyle.BackColor; r.ForeColor = SelectedRowStyle.ForeColor; r.Font.Bold = SelectedRowStyle.Font.Bold; } else { r.BackColor = style.BackColor; r.ForeColor = style.ForeColor; r.Font.Bold = style.Font.Bold; } } }
I try to mak a custom textbox. Something pretty easy to do but I can't figure out one thing. When I write my textbox in designmode I have this <custom:Textbox runat="server" ID="Textbox1">Test</custom:Textbox> The problem is The textbox is a webcontrol I added to my class [Parsechildren(false)] like this [Parsechildren(false)]
public class Textbox : WebControl { }
now the problem is I want to able to write the Text from the textbox between the beginningtag and andingtag but I want to disable every other control in designmode. When I add < between the tags then you can see you can write all tags between it. disable all controls except literal between the tage in designmode?
IŽll try to describe the problem in steps, as this might be the easiest to understand:
1. Page_Load is called when the page is requested, and calls a BuildTable() method
2. The BuildTable method creates a table which contains several textboxes
3. the user types some text in these textboxes
4. The userthen changes an item in a Dropdownlist on the page which is intended to change some other content on the page
5. Page_Load is called, and afterwards the DropDownListBookingType_SelectedIndexChanged(object sender,EventArgs e) method are called, and the content is changed
6. The text the user typed in the programmatically created textbox controls are lost!
Now how do I remain this text information? If just the SelectedIndex_changed method was called first, I could save it in session.. But as far as I can tell, all data on the page are discarded when you create a partial postback :(
I created some required field validators and a validation summary control. When I get an error it displays next to the control its validating and also inside the validation summary control. I only want the error to display in one place; inside the validation summary control. I tried making the required field validator invisible but then it didn't display anywhere.How can I get the errors to only display in the validation summary control? There is no need to display them twice it just clutters up the page.
I have a checkbox on my page that must be ticked and to validate it I am using a custom method as below. The problem is that I now put the checkbox and method within a Create User Wizard custom tempalte and when I submit the form it does not activate the validation check. If I put a normal button on the page outside the tempalte it works. Not sure If I a missing something. Perhaps I can activate it in Created User Event handler in code behind. All Button in my create user Event.
i have problem in using ajax control , till morning it everything was file , now when i add any new ajax control it shows the error Control canot be created because visual studio cannot find the control's type in the control assembly then if i press OK it says The operation could not be completed . invalid FORMATETC structure
I am creating a custom 'control' which is simply made of two html inputs [type=text] but can't figure out how the validation will work on the view end.
I want to treat these 2 inner controls as asingle control so when a error is added to modelstate both of these inputs are highlighted in the view and a single validation message is shown.
I've written a custom ASP.net control that descends from LinkButton and overrides the Render() method. I'm using it to replace ImageButtons in the site I'm working on so we don't have to have an image for each button. This control works fine, does the required post-backs etc., however it doesn't fire the validators in its validation group. This is obviously an issue. The code for the control is (condensed) as follows:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls;
Does anyone know why this would not be causing the validators to fire? I was under the impression that leaving all the other methods in LinkButton un-overridden would leave all the other functionality the same.
I want to validate and save the image along with other form value upon "registerBtn_click" , but the custom validation control of the image does not activate somehow... why?
I'm creating 14 textboxes for every day in a month and user can select the month so i have o create them dynamically. But i can't reach the values in the textboxes.
I'm creating textboxes in the Page_Load event and giving them values, however whenever I load the details I am getting the same output. I always seem to get the first output I got, even on subsequent searches.Here's what my code with the irrelevant information missing:
If DropDownList has two values (e.g. "Item 1" and "Item 2") and has autopostback enabled, first time it will generate and show "Item 1" in the textbox (the default value for the DropDownList), but if this is changed and the autopostback fires, the textbox does not change.I've tried getting around this by creating a new TextBox, overriding the "LoadPostData" function to prevent this from loading, which got around the issue there, but then I couldn't view the data afterwards.
Any idea how I could get around this? I may be approaching this in the wrong way.Edit: I've added another item to DropDownList that removes TextBox, so that it can be re-created again. It seems to show the correct data when it is re-created after being removed, but if I'm attempting to just edit it, this isn't updating correctly.Edit2: Here's the rest of the code for this page in case this helps at all. The objects which I'm having issues with are SBUName and SBUComments, which are both TextBoxes. The same issue is also happening for SBUClientDropdown but I believe the resolution will be similar:
DBHandler DBHandler = new DBHandler(); List<string> clients = new List<string>(); List<string> clientGroups = new List<string>();
I have 2 custom validator controls on my page but for some strange reason the Custom Validator for FileUpload control works and the custom validator for TextBox control does not work. Here is the aspx code:
I'm trying to add a validation support to DropDownList by creating a custom control inheriting from DropDownList:
public class MyDropDown: DropDownList, INamingContainer { private const string ValidatorID = "Validator"; private RequiredFieldValidator _validator;
[Code]....
My class implements INamingContainer because I wanted to avoid naming conflicts. Unfortunately when I try using this control I get the following exception:
Unable to find control id 'MDD' referenced by the 'ControlToValidate' property of 'MDD_Validator'
This is happening beause for INamingContainer FindControl(NamingContainerId) returns null.
When I remove INamingContainer implementation and set validator ID in the following way:
I have the following code to generate a table with the necessary textboxes. Now i need to populate those fields with the data from the table. If it makes it any easier, the individual textbox values are stored in a table with a reference to the main recordID
How can i populate the correct textbox with the appropriate column value?
I have a textbox in my application which has multiple validation on it e.g. RequiredFieldValidator, RegexValidation and CustomValidation. My page has several similar textboxes. So I just copy-paste and change id and controltovalidate properties and it is working.
Since similar tbxs are going to be used on another page as well, I think it would be nice to create my own custom TextBox control with built-in validation.
Here are two approaches I have found and tried:
1: Implement from IValidator perform my custom validation in Validate Method. As shown here: Self-Validating TextBox But it does not show how to implement client-side validation.
2: Create custom control that derives from TextBox and add asp.net built-in validators I need. As shown here:Custom TextBox. I tried the code and it works server/client side.
I like the first approach but don't know how to implement client-side validation. I know I need a client-side js function. I can do that. I know how to include my js file using Page.ClientScript class but don't know how to integrate all together and make it work.
I can create a UserControl or the second approach above but for now I am specifically looking to learn and implement client-side validation from custom control.
I am sure this question has been asked and answered; but I haven't managed to find it...
I am creating a very simple custom System.Web.UI.Control that has a few properties. I can define in my ASPX page the following tag and everything is happy:
what I need to do to make the XHTML validate. I always have
Content is not allowed between the opening and closing tag of element 'XXX'
Element 'XXX' is not supported
The name contains uppercase characters, which is not allowed
The code actually runs as expected, but I haven't manged to find a good example on how to do this correctly so that everything validates. In terms of implementation of the Custom Control, I just have all properties stubbed out at the moment, and looks something like:
[ParseChildren(true)] [PersistChildren(false)] public class MyControl : Control[code]....
Ultimately, Element is intended to build up a collection of Elements. Any insight on how to do this properly and have the XHTML validate
I have a little doubt about creating a login control in the master page within a contentplaceholder.
What i want is to create my own login control in the masterpage, using a loginview inside a contentplaceholder. The code is the following:
[Code]....
So far so good, but now i want to create the logic to do the actual login. In a regular aspx page i'll do it in the cmdlogin click event, but as i'm in the masterpage, i'm not sure if i can do that. I actually tried to do it in the code behind of the masterpage, but it didn't work. What can do?? do i ahve to use the asp.net login control?
When I implement Custom web control inheriting ASP.NET Menu Ajax is not working. I try to Override Render method and generate Li's rather than table.I try to use Control Adapters, but when I am not using adapters for some pages ithe base adapter is generating just hyperlinks with span. I do not want this way. So I can not use these.
I have an update panel which contains a number of controls. The update panel is triggered by the OnSelectedIndexChanged event of a dropdownlist called: ddlUSCitizenshipStatus. It works as expected when I selected a new item.
However, if I leave ddlUSCitizenshipStatus with the default value, and then click "submit" button on the page, the requiredfieldvalidators say there is an error on ddlUSCitizenshipStatus (which it should, as I never selected a value). So I then choose a value, the error message goes away on ddlUSCitizenshipStatus, however the updatepanel does not refresh. I've debugged this locally and the OnSelectedIndexChanged event for ddlUSCitizenshipStatus does not fire.
If I choose an item in the ddlUSCitizenshipStatus list a second time, the OnSelectedIndexChanged server event fires and the update panel refreshes and works as expected.The issue is, I have to select an item in ddlUSCitizenshipStatus twice, after failed validation, before the updatepanel it's sitting in updates.The submit button on the page looks like this:
[Code]....
If I remove my custom OnClientClick script, making the submit button look like this: [Code]....
The dropdownlist, update panel, and reguiredfieldvalidator all work as expected. However, I need to run that custom "ValidatePage()" script when the button is clicked.Below is what my ValidatePage script looks like. I've been troubleshooting this for more hours than I can count.... I hope someone is able to help me. Please let me know if you can figure out why ddlUSCitizenshipStatus doesn't update the updatepanel until the second click after a failed validation. [Code]....