ASP.NET Dynamic Radiobutton - How to add validation - asp.net

At the moment I have something like this
<asp:Repeater ID="rptEventsList" DataSourceID="srcQuestionList" runat="server">
<ItemTemplate>
<td><span><%# Eval("orderBy").ToString()%>)</span></td>
<td><%# Eval("question").ToString()%></td>
<td><asp:RadioButton ID="RadioButton1" runat="server" /></td>
<td><input name="question<%# Eval("orderBy").ToString()%>" type="radio" id="True" value="True" class="styled" /></td>
<td><input name="question<%# Eval("orderBy").ToString()%>" type="radio" id="False" value="False" class="styled" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
And in the code behind I capture the values as Request.Form("question1") for example and this all works fine.
Now I am wondering how to add validation to this, I think I have to apply changes to a RadioButton control but I can't see how I could add my dynamic RadioButton names in there with my id from stored procedure.
Also I would like to add the validation to the top of the screen as a validation summary.

Look into Validation Server Controls.

Related

Changing the cssclass for a <tr> using vb.net

I have this and I want to hide rows dynamically using vb.net codebehind.
I am using VS2010.
This is my table:
<table>
<tr id="FromDateRow">
<td><asp:Label ID="FromDateLabel" runat="server">From date:</asp:Label></td>
<td>
<input type="text" id="txtFromDateF" class="needs-datepicker" />
<asp:TextBox id="txtFromDate" CssClass="hidden" runat="server" />
</td>
</tr>
<tr id="ToDateRow">
<td><asp:Label ID="ToDateLabel" runat="server">To date:</asp:Label></td>
<td>
<input type="text" id="txtToDateF" class="needs-datepicker" />
<asp:TextBox id="txtToDate" CssClass="hidden" runat="server" />
</td>
</tr>
<tr>
<td><asp:Label ID="CustomerCodeLabel" runat="server">Customer Code</asp:Label>:</td>
<td><asp:DropDownList ID="CustomerCodeDropDownList" runat="server" AutoPostBack="False" /></td>
</tr>
<tr>
<td><asp:Label ID="CINumberLabel" runat="server">CI Number</asp:Label>:</td>
<td><asp:TextBox ID="CINumberTextBox" runat="server" /></td>
</tr>
</table>
Now I want to do something like this:
Select Case value
Case DisplayDates.FromDate
ToDateRow.CssClass = "hidden"
FromDateRow.CssClass = ""
Case DisplayDates.ToAndFromDate
ToDateRow.CssClass = ""
FromDateRow.CssClass = ""
Case Else
ToDateRow.CssClass = "hidden"
FromDateRow.CssClass = "hidden"
End Select
For some reason I cannot access the ToDateRow and the FromDateRow from my codebehind.
The objects you are trying to reference in codebehind (the relevant tr elements) needs to be defined as runat="server"
If CssClass is not a known property for the object instance try using the following:
rowObject.Attributes.Add("class", "hidden");
You must have
runat="server
tag in table and tr to access from code behind.
After comment :
ToDateRow.Attributes("class") = "CssClass";
Ok I solved it.
By adding the runat="server" i was able to access the row.
And then I found the "visible"-property.
Now it works just fine.
Thx for the help

alignmnet in radio button list asp.net vb

I'm confused with the alignment setting of the radio button list in asp.net, it shows in visual basic like this.
But if i compile it, it shows in my browser like this.
My code for this radio button list.
<td align="right" colspan="2">
<asp:RadioButtonList TextAlign="left" ID="RadioButtonList1" runat="server">
<asp:ListItem Text="Fixed Cost" Selected="true" Value="1"></asp:ListItem>
<asp:ListItem Text="Per Guest Charge" Value="2"></asp:ListItem>
<asp:ListItem Text="Percentage" Value="3"></asp:ListItem>
</asp:RadioButtonList>
</td>
How come this happened? I want to get the view like in visual basic, please help.
--- Update ---
This is the result if i change the alignment into "right".
Here's the HTML.
<tr>
<td align="right" colspan="2">
<table id="ctl00_MainContent_RadioButtonList1" border="0">
<tr>
<td><input id="ctl00_MainContent_RadioButtonList1_0" type="radio" name="ctl00$MainContent$RadioButtonList1" value="1" checked="checked" /><label for="ctl00_MainContent_RadioButtonList1_0">Fixed Cost</label></td>
</tr><tr>
<td><input id="ctl00_MainContent_RadioButtonList1_1" type="radio" name="ctl00$MainContent$RadioButtonList1" value="2" /><label for="ctl00_MainContent_RadioButtonList1_1">Per Guest Charge</label></td>
</tr><tr>
<td><input id="ctl00_MainContent_RadioButtonList1_2" type="radio" name="ctl00$MainContent$RadioButtonList1" value="3" /><label for="ctl00_MainContent_RadioButtonList1_2">Percentage</label></td>
</tr>
Have you tried setting <asp:RadioButtonList TextAlign="right" - you have TextAlign="left"?
I think your result HTML (being <table> in your addition above) is not guaranteed - ASP.NET may emit <span> in other cases/browsers.
Your best option seems to be assigning a CssClass <asp:RadioButtonList ... CssClass="yourClass"> in combination with some jQuery or direct styling of the possible elements.
See this broader discussion and also the accepted answer in this thread.

Can RequiredFieldValidator have an effect on other elements on the page?

I have a basic form on my page with the following textbox and validator. When the validator is visible, there is a background image that is displayed. I would also like to be able to change the border of the textbox. Is that possible? If not in the ascx file, would it be possible in Javascript?
<tr>
<td class="FormLabel">First name*</td>
<td class="FormInput">
<asp:TextBox ID="txtFirstname" runat="server" Width="180" MaxLength="30" fieldname="FIRSTNAME" tablename="TblName"></asp:TextBox></td>
<td class="FormValidation">
<asp:RequiredFieldValidator ID="valFirstname" runat="server" ControlToValidate="txtFirstname" CssClass="ValidatorError"> </asp:RequiredFieldValidator></td>
</tr>
Thanks
If you would use a ValidatorCalloutExtender of the ASP.NET-Ajax-Control-Toolkit, this would be simple by applying an appropriate HighlightCssClass.

How can I determine whether any checkbox is checked?

I have some checkboxes in vb.net code.
<tr>
<td colspan="2">
<asp:CheckBox ID="chkbxCreateAmendOrg" runat="server" Checked="False" Text="Create/Amend Organisation" />
</td>
<td colspan="2">
<asp:CheckBox ID="chkbxCreateAmendCPUser" runat="server" Checked="False" Text="Create/Amend CP User" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="chkbxDeleteOrg" runat="server" Checked="False" Text="Delete Organisation" />
</td>
<td colspan="2">
<asp:CheckBox ID="chkbxDeleteCPUser" runat="server" Checked="False" Text="Delete CP User" />
</td>
</tr>
I want to give alert to user if they have not selected atleast one. Can i have jquery code for this
You can select all the not checked checkboxes and check the length or size() of the jQuery object:
if ($('input:checkbox:not(:checked)').length > 0) {
// some checkboxes not checked
}
Something like this should get it done...
$(document).ready(function() {
// get all checked
var checkboxes = $("input:checkbox:checked");
if(checkboxes.size() == 0)
alert("Please mark a checkbox!");
});
The following code print an alert for each checkbox not flagged:
$("input:not(:checked)").each(function(){
alert( $(this).attr("id") + " isn't checked!" );
});
See also the selectors
I advice use jQuery Validation plugin. It is very simple to use and so elegant (nice way to display error messages).
Check that example. (There are two radio buttons which one should be checked.)

How to access data from html controls in asp.net

How do I access data from html in asp.net in the .cs (code behind) file?
In .aspx page I have:
<tr>
<td>Username:</td><td><input id="username" type="text" /></td>
</tr>
<tr>
<td>Password:</td><td><input id="password" type="password" /></td>
</tr>
<tr>
I know I can convert this to something like:
<tr>
<td>Username:</td><td><asp:TextBox ID="username" TextMode="SingleLine" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password:</td><td><asp:TextBox ID="password" TextMode="Password" runat=server></asp:TextBox></td>
</tr>
This will allow me to access the controls via IDs.
However I was wondering if there was a way of accessing data without using asp.net server-side controls.
Give the inputs a name as well as an id and you will be able to get the values from Request.Form. Inputs without names are not sent back with the form post.
<input id="username" name="username" type="text" />
<input id="password" name="password" type="password" />
var username = Request.Form["username"];
var password = Request.Form["password"];
Add runat="server" to the controls, and then you can access them from the code-behind almost as if they were <asp:______ /> controls.
ASP.NET controls, are in essence HTML controls wrapped, so an asp:Button will render as a input Html control.
Some web developers prefer using Html controls due to the smaller size.
Therefore each HTML control will map to a asp server control.
As the previous answer, from Joel, add the runat="server", then the control can be referenced by the ID from the code behind.
This is your code:
<tr>
<td>Username:</td>
<td><input id="username" type="text" /></td>
</tr>
<tr>
<td>Password:</td>
<td><input id="password" type="password" /></td>
</tr>
You can just add the runat="server" attribute to the Html controls:
<tr>
<td>Username:</td>
<td><input id="username" runat="server" type="text" /> <!--NOTE THE RUNAT="SERVER"--></td>
</tr>
<tr>
<td>Password:</td>
<td><input id="password" **runat="server"** type="password" /></td>
</tr>
Now you can access the controls in asp.net.

Resources