ASP .NET - Run required field validator and calloutextender in code behind? - c#

I am using ASP .NET (C#) and have a page with a listview linked to a sqldatasource.
The listview has a InsertItemTemplate which contains many textboxes. I want to make all the textboxes required via the RequiredFieldValidator (and the ValidatorCallOutExtender).
Is there a way to do this in the codebehind instead of the aspx page?
Ideally I would like the page to validate each control with the same method with only the error message changing.

I take it your question is about dynamically adding controls (and not about codebehind validation).
I didn't know and ran a few tests. It is possible to simply add (Validator)controls to the Controls property of the Form element, but controlling the order is not so simple (no Controls.Insert()).
So my advice would be not to use a plain TextBox but a UserControl. You could probably use Search&Replace to fix up the ItemTemplate.

Related

asp.net runat="server" c#name does not exist in the current context

I have the following asp.net div setup on my page:
<div runat="server" id="ImageContainer">
<img src="images/<%# Eval("Image") %>" class="ArticleImage"/>
</div>
and I know this is being created at the server as when I view it in developer tools its id is:
ctl00_body_ArticleInfoRepeater_ctl00_ImageContainer
which is being created by the server.
I now want to edit the Css under certain conditions by doing the following:
ImageContainer.CssClass = "invisibleClass";
My problem is, in the C# code I am getting the following error:
The name 'Image container' does not exist in its current context.
I have no idea why this is happening, as it clearly does exist.
Any ideas?
Are you using some databinding control such as GridView, Repeater or some other?
The syntax <%#....%> represents data binding which works if it is placed inside some data binding control.
In such case you cannot access "ImageContainer" control directly. You have to search through parent control.
Since you haven't mentioned what is parent control of "ImageContainer" it's hard to give code sample here... Though here is example how it can be done in GridView
Incase, if you haven't used DataBindingControl then I would recommand to check yourpage.aspx.designer.cs and you should be able to find control name there!
Hope this will be helpful.
That's because you're trying to reference a dynamically created object (obiviously inside a repeater). Firstly, you shouldn't set ID for dynamic objects created during runtime. IDs have to be unique, or else you'll run into problems.
Secondly, you need to get the parent object (probably the repeater itself or the container?) and then traverse the collection of child controls to find the one you're after. There are numerous answers about how to find a control in an asp.net webforms, so just google for a while and I'm sure you'll get the code.
For instance find control
Controls placed on data list,repeater,grid-view are not accessed directly like other server controls placed on the page. If you want to access it through code behind you can access it on Data-bound or Item_command event of the repeater control because these controls itself act as containers for other controls placed on them.
you can use
e.Items.findControl("controlID") to access a particular row controls.
I recommend you to study these two events of repeater control.
if you want to change class of all div with name imagecontainer , you can use javascript or jquery for doing it with few lines of code.

Use one control multiple times in ASP.NET

What I have is few dropDownLists in few different templates of FormView.
And of course I can't get access to any of those.
What I want to do is get the same behavior for every DropdownList.
Is there any way to declare that DropDownList globally and reuse it or it should be done somehow with FindControl?
User control declarative syntax is very similar to syntax used to create an ASP.NET Web page.
The primary differences are that the user controls use an # Control directive in place of an # Page directive, and that the user controls do not include the html, body, and form elements around the content.
MSDN/creating usercontrol

Dynamic Drop Down List ASP.net

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.

How do I dynamically create new Hyperlinks in ASP.NET?

I'll explain what I'm trying to achieve:
I want to have a situation where I can create as many controls as I want by creating them in a loop in the code behind. I can do this while using PHP, by mixing the PHP code and the HTML code. This allows me to generate actual HTML tags dynamically.
In ASP.NET, I haven't found a way to replicate this functionality.
I've thought about using a loop in the code behind on something like the Init() function to create an array of new() objects, setting their attributes and hoping it is passed into the aspx file, but it didn't work.
How do I do this?
If you want to creat Dynamically ASP.Net Hyperlink control
You can simply do this:
HyperLink hyp = new HyperLink();
hyp.ID = "hypABD";
hyp.NavigateUrl = "";
Page.Controls.Add(hyp);
Well, keep in mind there's a difference between Hyperlinks and LinkButtons in ASP.Net
If you just want Hyperlinks, you can create as many as you want by creating a HyperLink object, creating new objects and then adding them to some control's Controls collection like panel or span or something.
LinkButtons are a little different. You have to use a Repeater control if you want to create a link that posts back to the server. (Well, you don't have to but the alternative is a hack job.)
Hyperlinks can be created by using a Repeater control as well and, if possible is the method I would recommend.
There are lots of ways to do this in asp.net:
If you really wanted to, you could put a loop using <% %> tags directly into the aspx markup, just like with php or classic asp. This is not recommended.
You can put a placeholder or panel control on your form, create as many hyperlink controls or anchor tags as you want in your code behind and add them to the placeholder's controls collection. This is a little better, but still not optimal.
You can put a reasonable number of hyperlink controls on the page, set their visible property to false by default, and "enable" the ones you need. This is preferred to #2 because of some oddities with asp.net page lifecycle.
These links come from somewhere, and that somewhere is usually a database or other reasonable datasource. So make it data driven — bind that datasource to a control like a repeater, gridview, details view, or similar. This is probably your best option.
If you want to generate controls dynamically & don't need to have those PostBack to the server (i.e. when a control is clicked/changed, it will come back to the same page) - you could use controls in System.Web.UI.HtmlControls namespace.
e.g.HtmlAnchor link = new HtmlAnchor();
link.href = "www.stackoverflow.com";
Page.Controls.Add(link);
Hope this gives you enough background.
EDIT: You can use the above code in a loop & replace the fixed value with what comes from a database/object.

UserControl - accessing textbox within UserControl within a web form

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.

Categories

Resources