I've created a web form programmatically using asp.net repeater, where one of the items/fields is an MSCaptcha control. On postback, I would like to validate the captcha. Been struggling to access it since the captcha field/control is inside the Repeater and this.FindControl("captchaid") returns null. Any ideas?
Also, I am able to access all the form values including captcha text through Request.Form["field id"]. What would be nice is to be able to actually validate the captcha and not just capture the value.
If you're using this.FindControl (as stated) you will search for controls in the current page or usercontrol, not in the repeater item. You will need to loop through the repeater items and perform the FindControl on the RepeaterItem instance until you find your captcha control. (Or google to find an implementation of FindControl that is recursive, though I would say this is probably less optimal).
Related
How can I create a dynamic drop down list without using AutoPostBack. I ask because I just want to change the value of what the second drop down list displays and then the user clicks a button and it submits. But If I use AutoPostBack then it postbacks to page and runs code that shouldn't be run until that final box has been selected. (I use Postback in the other part of my program so using !IsPostBack isnt a option.) I looked at Javascript but ASP generates all of its controls names at runtime. What can I do? I have looked at the Ajax CascadingDropDown control but my problem with that is it contained in a XML file or a Database, I need this to be contained inside my Page. Any Ideas?
You can use the CascadingDropDown control from the AJAX Control Toolkit
Maybe this example will help? It's part of the ASP.NET AJAX Control Toolkit available here.
You can use AJAX to get the values for the second drop down list, based on the selected value of the first. Add a onchange event handler on the client-side to the first drop down list that makes the AJAX call and fills the second on success.
Greetings,
I created a aspx page that uses a master page. Inside I have a control, let's say txtName and I have a submit button, that when I press it it will submit the text in the txtName to another page.
The problem is that the Request.Form key does not have the txtName, instead it something like clt00$Container0&txtName.
Isn't there a way to easily find the key that I want to retrieve from there?
You can implement the behaviour via Cross Page Postback. Then you can access the value via PreviousPage.FindControl("txtName") or via a custom property (see section "Using Property to expose and Consume values of TextBox" in the linked article above)
Make sure they r with in the same form when, it
renders onthe client. If not u will not get on server
look view source n see where u master page control
is
I'm adding controls at run-time to the Page Form object.
Each control has an Id.
On postback, I want to be able to access these controls and their values.
Currently, when I do a postback, the form has no controls in the Form.Controls Collection.
How can I rectify this?
Is this only possible if you add the controls to the page every time the page loads?
Dynamically added controls need to be added with every page load or else they will be lost.
However the viewstate of these controls can be maintained as long as they always get added with the same ID.
I believe you have to add the controls dynamically in order to access them on postback. So if you add a textbox dynamically, your event handler can't retrieve its value unless you add it again.
EDIT: One workaround I used was to add a predetermined set of server controls to the page and then use JavaScript to hide/show those elements. You avoid postbacks and you avoid the unnecessary tom-foolery associated with retrieving values from dynamically added server controls. Of course, this limits you to a predefined number of controls.
This was a big pet peeve of mine with ASP.NET web forms and is a factor in my decision to explore ASP.NET MVC - no more viewstate/postback mess.
I have created my own control which contains TextBox control as a child control. this textbox has autopostback set on. when the postback is called I dont get any text in request.querrystring.
where should I specify text entered into texbox will be send to response ? or is there any easier way of sending this information? I want to have the entered text once again in textbox after postback.
thank you
For dynamically created controls, you need to create them and add them to the control tree in the Page_Init() event; Page_Load() is too late, and Page_PreRender() is way too late.
Once in the control tree, the runtime will restore the state of the control for you after a postback. You can access the contents of the control using myTextBox.Text -- you don't need to use Request.QueryString
For a control that only contains a TextBox, things would be much easier if you used a user control rather than an INamingContainer. That way, you could just use <asp:TextBox> in your .ascx markup.
A custom control? Any posted control like this loads its data in LoadPostData method. You can override this in your custom control class, and access the value from teh control via:
var text = postDataCollection[textboxID];
The textbox within your custom control has to have an ID.
If you are talking something else, let me know the specifics.
Thanks.
(TextBox)Control.FindControl("control id")
get dynamic control's value in postback , Request.Form("control client id")
I am using c#.net
I have different views within my webform, these all generally display different information except for three textboxes (arrival / seen / depart time). To try and cut down on code, I have created a UserControl which contains these three textboxes.
I have referenced the UserControl at the top of my webform and also within each view.
<%#Register TagPrefix="uc1" TagName="userTimes" Src="~/usercontrols/userTimes.ascx"%>
<uc1:userTimes id="userAppointmentTimes" runat="server"></uc2:userTimes>
can’t seem to access the textboxes from the code behind. I need to firstly populate the textboxes and also hold any updated information to be re-inserted back into the database if changed.
Also each textbox has two Validation controls:
First makes sure it is in time
format HH:MM
Second makes sure the arrival is
before the seen time etc
My two questions are:
How do I access the UserControl from
the code behind? I have read that I
need to use the FindControl but I
don’t understand why, when I know
what it is called.
Do I undertake the validation
(server side) within the UserControl
code behind or the webform code
behind?
Thanks in advance for any help.
Clare
1.) You can access the User Control by its ID from the page's code behind - userAppointmentTimes in your case. To access your TextBoxes within the webform you need to use the FindControl-Method at the User Control level. So something like userAppointmentTimes.FindControl("WhateverTextBoxID") should work. You need to cast the result to TextBox of course.
You can't access the text boxes because ASP.Net does not automatically expose them for you. So alternatively you can provide public properties to set/get values to/from your textboxes inside your user control.
Within the user control, you can access your textboxes by their IDs.
2.) Put the validation controls inside your user control.
By webform you mean it's all inside the asp.net form-tag or do you have an asp.net form like FormView nested inside? If the latter is true you need to use FindControl at the FormView level - formView.FindControl("userAppointmentTimes"). Otherwise the user control is accessible from page level via its ID.