How can I determine whether any checkbox is checked? - asp.net

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.)

Related

How to make asp .net to forget entered text in fields?

Everywhere I read questions about how to remember it, but I want to reset it!
I use this code:
<table>
<tr>
<td>Your name:</td>
<td><asp:TextBox runat="server" ID="user_name" MaxLength="20" AutoCompleteType="None" /></td>
</tr>
<tr>
<td><table><tr><td>Captcha:</td><td><img src="turing.aspx" alt="Enable images to see captcha image" /></td></tr></table></td>
<td><asp:TextBox runat="server" ID="txt_captcha" MaxLength="5" AutoCompleteType="None" /></td>
</tr>
<tr>
<td>Email for new messages reports:</td>
<td><asp:TextBox runat="server" ID="txt_eml" MaxLength="100" AutoCompleteType="None" /> (*this is not required)</td>
</tr>
<tr>
<td>Message text:</td>
<td><asp:TextBox runat="server" ID="comment_text" Columns = "30" Rows = "5" Wrap = "true" TextMode="MultiLine" AutoCompleteType="None"/></td>
</tr>
<tr>
<td colspan = "2">
<asp:Button runat="server" ID="Button1" Text="Add message" OnClick="HandleAddMessage" CausesValidation="true" ValidationGroup="second_group" />
</td>
</tr>
</table>
At HandleAddMessage I even reset those manually that way: user_name.Text = txt_eml.Text = txt_captcha.Text = comment_text.Text = string.Empty;
But after message added, if I refresh the page old values comes back.
What I can do to prevent it?
Once the user has submitted the form the first time, just response.redirect to the same page again. It effectively resets the page, so if they do refresh it, they won't post back the same data. It would also mean that your fields will all be reset since the page has effectively reloaded.

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

How to add blank row in grid view?

I am new to the ASP.NET i am binding one list of data object to the grid view. I want to display blank row after each record in grid view so i have done this by as below in code behind
List<DatabaseDTO> lstdatabase= new List<DatabaseDTO>();
foreach(int jobNumber in JobnumberList)
{
DatabaseDTO dataObject = new DatabaseDTO();
dataobject = GetDatabaseData(jobNumber);//Method to retrieve data and return data object
lstdatabase.Add(dataObject);
lstdatabase.Add(new DatabaseDTO());
}
gridView.DataSource = lstdatabase;
gridView.DataBind();
it's working correct i am getting the desired blank row in the grid view but i know this is not right way because i am adding object to the list so i can add the blank row in place of that i would very much like to adjust this blank row from the aspx page. I know there is another way using the DataTable but it is also not very good because it also adds the unnecessary records to the DataTable. So any other work around or way to solve this would be very great. Thank you.
Try This
<div>
<asp:DataList ID="DataList1" runat="server">
<ItemStyle ForeColor="#4A3C8C" BackColor="#E7E7FF"></ItemStyle>
<HeaderTemplate>
<table width="900px">
<tr>
<td width="300px">
<b>Name</b>
</td>
<td width="300px">
<b>Account No</b>
</td>
<td width="300px">
<b>Company</b>
</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="900px">
<tr>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "Name")%>
</td>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "AccountNo")%>
</td>
<td align="left" width="300px">
<%# DataBinder.Eval(Container.DataItem, "Company")%>
</td>
</tr>
<tr>
<td align="left" width="300px">
<br />
</td>
<td align="left" width="300px">
<br />
</td>
<td align="left" width="300px">
<br />
</td>
</tr>
</table>
</ItemTemplate>
<HeaderStyle Font-Bold="True" ForeColor="#F7F7F7" BackColor="#4A3C8C"></HeaderStyle>
<SeparatorTemplate><br /></SeparatorTemplate>
</asp:DataList>
</div>
You can't have an empty row in the Datagrid if it isn't present in the data source. You have to think that after all the grid data is just a representation of your data source, if there is a empty row, the grid will show it, if there is not, it wont.
Write a stored procedure to get Output Parameter from sql server and bind to grid view if record is not there..

displaying custom message on custom errors in validation summary control on client side?

I am using validation summary control to display error messages of asp.net validation controls.
There are some validations in page for which validation controls are not being used. I am using custom javascript and jquery code for these. Kindly guide how I can display messages of these errors in validation summary control along with asp.net validation controls.
The key is to use the Custom Validation control. This control supports client side scripting (so you can still use your javascript code to do it) but still ties into the validation framework asp.net provides.
From the microsoft article, something like this:
<SCRIPT LANGUAGE="JavaScript">
function validateLength(oSrc, args){
args.IsValid = (args.Value.length >= 8);
}
</SCRIPT>
<asp:Textbox id="text1" runat="server" text="">
</asp:Textbox>
<asp:CustomValidator id="CustomValidator1" runat=server
ControlToValidate = "text1"
ErrorMessage = "You must enter at least 8 characters!"
ClientValidationFunction="validateLength" >
</asp:CustomValidator>
As Patrick said you can add the custom validator controls in place of your custom javascript/jQuery code. And then accordingly you can add the summary validation control. Also you can change the error message in your javascript code. Please check the below code for your reference.
<script type="text/javascript">
function validatetxtLength(source, args)
{
var txtVal=document.getElementById('<%=txtusername.ClientID %>').value;
if(txtVal=="")
{
document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Please Enter the User Name");
args.IsValid=false;
}
else if(txtVal.length>9)
{
document.getElementById('<%=custxtValidator.ClientID %>').setAttribute("errormessage","Username must have less than 10 characters");
args.IsValid=false;
}
else
{
args.IsValid=true;
}
return;
}
</script>
<div>
<table cellpadding="0" cellspacing="0" border="0" width="712px">
<tr>
<td colspan="3" align="center">
Validator Testing
</td>
</tr>
<tr>
<td>
Please Enter your User Name:
</td>
<td>
<asp:TextBox ID="txtusername" runat="server" Width="150px"></asp:TextBox>
</td>
<td>
<asp:CustomValidator ID="custxtValidator" runat="server" ErrorMessage="User Name must have less than 10 characters"
Text="*" ForeColor="Red" ClientValidationFunction="validatetxtLength"></asp:CustomValidator>
</td>
</tr>
<tr>
<td colspan="3" align="center">
<asp:Button ID="btnsubmit" runat="server" Text="Submit" />
</td>
</tr>
</table>
<asp:ValidationSummary ID="ValidationSummary1" HeaderText="Please check below validations:"
runat="server" DisplayMode="BulletList" EnableClientScript="true" />
</div>
This might be useful for you.

javascript validation: comparing 2 text box values

I have a table with 5 rows and 2 columns. Each cell contains a text box. I want to show error if one of the text boxes in each column is empty. I want both text boxes in a row shld be filled or both shld empty.
How can i do this via Asp.net validation controls?
I want to extend CompareValidator so that it will validate only if the controlToValidate and controlToCompare both have some text in it or both are empty.
You would need to use a CustomValidator and handle its ServerValidate event (and, optionally, its ClientValidationFunction for client-side validation). You could do one on the page and check all the rows, or you could have one per row and use the ControlToValidate property to give you context to the row you're validating.
Any example of the client-side validation is going to depend on your layout and any JavaScript framework that you're using. It might look something like this:
<table>
<tr>
<td><asp:TextBox runat="server" ID="TextBox11" /></td>
<td><asp:TextBox runat="server" ID="TextBox12" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox21" /></td>
<td><asp:TextBox runat="server" ID="TextBox22" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox31" /></td>
<td><asp:TextBox runat="server" ID="TextBox32" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox41" /></td>
<td><asp:TextBox runat="server" ID="TextBox42" /></td>
</tr>
<tr>
<td><asp:TextBox runat="server" ID="TextBox51" /></td>
<td><asp:TextBox runat="server" ID="TextBox52" /></td>
</tr>
</table>
<asp:CustomValidator ID="TextBoxPairValidator" runat="server" ControlToValidate="TextBox11" ClientValidationFunction="TextBoxPairValidator_ClientValidate" />
<script type="text/javascript">
(function () {
window.TextBoxPairValidator_ClientValidate = function (sender, args) {
var other = document.getElementById(sender.id.slice(0, -1) + '2');
args.IsValid = (sender.value === '' && other.value === '')
|| (sender.value !== '' && other.value !== '');
};
}());
</script>
That example obviously assumes a fairly simple layout, and fairly static naming (i.e. if your controls are in a naming container, you may not be able to use the ID trick to go from one text box to the other). Hopefully that's enough to get you started.

Resources