net application. I have one gridview with checkboxes.Whenever checkbox is checked i want to send email to users. My columns of gridview are id,name,emailid etc. Whenever checkbox is checked i want to send email. I written javascript code to catch up all the emailids from gridview and pushing all the email ids to the array. I am confusing how to take these id's to the server side.
This is my button.
<asp:Button ID="Button1" class="submitLink blueButton" runat="server" Text="GETID" OnClientClick="javascript:sendEmail()" OnClick="Button1_Click" />
Using below line of code I am able to get the required email id's
$('#<%= gdvRegretletter.ClientID %> input[type="CheckBox"]').each(function () {
if ($(this).closest('tr').find('input[type="checkbox"]').prop("checked") == true)
email.push($(this).closest('tr').find('td:eq(3)').text().trim());
});
Array email will be capturing all the required email id's but i want these values inside the Button1_Click event in server side. May i have some idea to achieve this? Thank you for your time.
You could loop all the rows in the GridView and see which CheckBox has been checked. In this snippet the email address is also displayed in the GridView in a Literal, but there are a lot of other ways do get the email address, either from the GridView or it's original source.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="Id">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server" Text='<%# Eval("email") %>'></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Button1_Click(object sender, EventArgs e)
{
//loop all the rows in the gridview
foreach (GridViewRow row in GridView1.Rows)
{
//find the checkbox with findcontrol and cast it back to one
CheckBox checkbox = row.FindControl("CheckBox1") as CheckBox;
//is it checked?
if (checkbox.Checked == true)
{
//do the same for the label with the email address
Literal literal = row.FindControl("Literal1") as Literal;
string email = literal.Text;
//send email
}
}
}
Related
I have an asp:CheckBox column in an asp:GridView. I am trying to uncheck previously checked CheckBox when another one is checked using javascript.
I have seen code dealing with 2 checkboxes but not in an asp:gridview.
I am new to javascript so any help would be appreciated.
Here is the HTML code:
<asp:GridView ID="GridViewGroups" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRow2" class="icheckbox" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
I am working on a web page.
In that web page there are 4 text boxes and each one has required field validator.
The web page has 3 buttons.
on clicking on 1st button 1st & 2nd required field validations should fire.
and on 2nd button 2nd and 3rd required field validator should fire
just like this on 3rd button 3rd and 4th should fire.
The problem is , since the one required field validator is invoked with two buttons i cant have a validation group name .
can anybody please suggest a solution .
it will be really helpful.
You can use JS, where you validate specific groups. For each button create separate validation method.
<script type="text/javascript">
function validateGroup12() {
var g1 = Page_ClientValidate("vgroup1");
var g2 = Page_ClientValidate("vgroup2");
if (!g1 || !g2) return false;
return true;
}
</script>
.aspx edit RequiredFieldValidator and set groups name into ValidationGroup
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ValidationGroup="vgroup1,vgroup2" Display="Dynamic">
Have two validators, instead of one validator, for Textbox 2 and Textbox 3 with the validation groups properly set to the corresponding button validation group.
Even though this is going to duplicate the validator, this simplifies your logic.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ValidationGroup="vgroup1" Display="Dynamic">
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ValidationGroup="vgroup1" Display="Dynamic">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="TextBox2" ValidationGroup="vgroup2" Display="Dynamic">
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="vgroup1" />
<asp:Button ID="Button2" runat="server" Text="Button" ValidationGroup="vgroup2" />
I'm developping a form page in a WebForms application.
I have a RadioButtonList and a Panel, and I wanted the panel to open when a especific value of the RadioButtonList was selected, how do I do this?
I've tried with AutoPostBack but I didn't like how it worked (running the page again), are there any other solutions?
Some of the places I searched for an answer recommended using the AutoPostBack property, but I personally didn't like it and so I had to find another way for it to work and so I decided to use JavaScript/JQuery...
This is an HTML sample code of an example of a RadioButtonList and a panel that opens when a chosen option on it is clicked (the value "Required" causes it to open and "Not Required" to close in this case):
<div>
<p class="space">3.2. ACCOMMODATION (*)</p>
<asp:RadioButtonList ID="accomodation" runat="server" RepeatDirection="Horizontal" RepeatLayout="Flow" Width="500px">
<asp:ListItem Text="Not Required" Value="Not Required"></asp:ListItem>
<asp:ListItem Text="Required" Value="Required"></asp:ListItem>
</asp:RadioButtonList>
<asp:Panel ID="PanelAccommodation" runat="server">
<p>
Number of nights (*):
<asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" ControlToValidate="numberOfNights" ForeColor="red" ErrorMessage="<- Required"></asp:RequiredFieldValidator>
</p>
<asp:TextBox ID="numberOfNights" runat="server" Width="50px"></asp:TextBox>
<ajaxToolkit:FilteredTextBoxExtender ID="FilteredTextBoxExtender1" TargetControlID="numberOfNights"
FilterType="Numbers" runat="server">
</ajaxToolkit:FilteredTextBoxExtender>
<p>Preferred Hotel:</p>
<asp:TextBox ID="preferredHotel" runat="server" Width="450px"></asp:TextBox>
<p>Preferred Hotel URL:</p>
<asp:TextBox ID="preferredHotelURL" runat="server" Width="450px"></asp:TextBox>
</asp:Panel>
</div>
The Script I used:
$(document).ready(function () {
$("#MainContent_PanelAccommodation").hide("fast");
$('#<%= accomodation.ClientID%>').find('input:radio').click(function () {
var selVal = $('#<%= accomodation.ClientID %>').find('input:checked').val();
if (selVal == "Required") {
$("#MainContent_PanelAccommodation").show("fast");
}
if (selVal == "Not Required") {
$("#MainContent_PanelAccommodation").hide("fast");
}
})
});
In this Script I use the first
$("#MainContent_PanelAccommodation").hide("fast");
to make sure that when the page runs the panel is hidden and will only open when/if "Required" is selected...
Another thing you may struggle with is what ID to put in the function since as you can see the panel ID is "PanelAccommodation", but the ID I use in the function is "MainContent_PanelAccommodation" since that's how it's recognized on your browser (to check this just select the panel's position and inspect element) you'll notice that the ID becomes "MainContent_PanelAccommodation" because it inherits from your asp:Content ContentHolderID...
Hope this helps ;) any questions just ask...
I have an ASP.NET 4.5 web form with inputs for a physical and mailing address. After entering a physical address, a user can tick a checkbox to fill in the mailing address if the information is the same.
This occurs via an OnClick function tied to the checkbox, which basically does this (I left out additional code):
document.getElementById("<%= txtMailAddressStreetNumber.ClientID%>").value = document.getElementById("<%= txtPermAddressStreetNumber.ClientID%>").value;
Each of the address textboxes has a RequiredFieldValidator. When I submit the form, it passes client-side validation and posts back to the server. There it fails (Page.IsValid is false). The textboxes that were populated by the OnClick function fail server-side validation unless I manually populate them.
I've tried calling Page_ClientValidate('FormGroup'); (FormGroup is the name of the validation group) after assigning values, and doing this doesn't trigger any validation error messages for the textboxes. However, when I submit the form (postback), the values inserted via Javascript are all cleared and I have to manually enter them.
Code in the .aspx page:
<asp:Label CssClass="mainlabel" ID="lblPermAddressZipCode" runat="server" Text="Zip Code" AssociatedControlID="txtPermAddressZipCode" />
<asp:RequiredFieldValidator ControlToValidate="txtPermAddressZipCode" ID="RequiredFieldValidator10" runat="server" ErrorMessage="*" Display="Dynamic" ValidationGroup="FormGroup" CssClass="formerror" />
<asp:TextBox ID="txtPermAddressZipCode" Width="100" onclick="openDialog(1)" ReadOnly="true" CssClass="readOnlyTextBox" runat="server" />
<asp:CheckBox ID="cbPermIsMailAddress" onclick="UsePermanentAsMailingAddress()" runat="server" /><small>(same as above)</small>
<asp:Label CssClass="mainlabel" ID="lblMailAddressZipCode" runat="server" Text="Zip code" AssociatedControlID="txtMailAddressZipCode" />
<asp:RequiredFieldValidator ControlToValidate="txtMailAddressZipCode" ID="RequiredFieldValidator15" runat="server" ErrorMessage="*" Display="Dynamic" ValidationGroup="FormGroup" CssClass="formerror" />
<asp:TextBox ID="txtMailAddressZipCode" ReadOnly="true" onclick="openDialog(2)" CssClass="readOnlyTextBox" Width="100" runat="server" />
Javascript:
function UsePermanentAsMailingAddress() {
//only copy if the address is complete
if (document.getElementById("<%= permCityStateZipId.ClientID %>").value != '') {
//exit if the box was unchecked
if (!$("#<%= cbPermIsMailAddress.ClientID%>").is(':checked')) {
return;
}
document.getElementById("<%= txtMailAddressZipCode.ClientID%>").value = document.getElementById("<%= txtPermAddressZipCode.ClientID%>").value;
//the ID value is the answer
document.getElementById("<%= mailCityStateZipId.ClientID%>").value = document.getElementById("<%= permCityStateZipId.ClientID %>").value;
//Page_ClientValidate('FormGroup');
}
else {
$("#<%= cbPermIsMailAddress.ClientID%>").removeAttr('checked');
alert("Please enter a complete permanent address first.");
}
}
Since the ReadOnly property is set in the .aspx, any changes made to it client-side will be ignored when it's posted back. If you absolutely need to have the textboxes read-only, then remove the property from your .aspx and set it with JavaScript on page load:
$(document).ready(function () {
$('#<%= txtMailAddressZipCode.ClientID%>').prop('readonly', true);
});
This way the user won't be able to directly change the textbox (I assume you have some other way of filling the textbox), but any changes will be posted back rather than the original (empty) values.
In my Jsp I have two text boxes txt1 and txt2
How can I validate these two text boxes ( txtboxes won't be same...)?
(I need an alert "Both the places are same")
Here is the code:
<asp:TextBox runat="server" ID="txt1"></asp:TextBox>
<asp:TextBox runat="server" ID="txt2"></asp:TextBox>
I would suggest you add a cssClass to them. So you have easier time selecting them. Here is an example:
Mark up
<asp:TextBox CssClass="compare" runat="server" ID="txt1">test</asp:TextBox>
<asp:TextBox CssClass="compare" runat="server" ID="txt2">test</asp:TextBox>
Javascript
var elements = $('.compare');
if (elements.eq(0).html() === elements.eq(1).html()){
//do stuff
}
Fiddle Demo
Remember this only does client side validation.