I have a dropdownlist. I want whenever user selects India, it should go to the next section of the page and if it selects other than India, it should go to the some other section. And also it should only move ahead if the checkbox is checked, Otherwise it should ask to check the checkbox first. Please see the code for your reference:-
<asp:DropDownList ID="ddlCountry" runat="server" class="txtfld-popup" Width="251">
<asp:ListItem Text="Enter your Country of Residences" Value="0"></asp:ListItem>
<asp:ListItem Text="India" Value="1"></asp:ListItem>
<asp:ListItem Text="USA" Value="2"></asp:ListItem>
<asp:ListItem Text="Other" Value="3"></asp:ListItem>
</asp:DropDownList>
Also see the checkbox code:
<asp:CheckBox ID="checkcountry" runat="server" />
I confirm that I am a resident of the selected jurisdiction.
<div id="errordiv" runat="server" style="color: #cf060d; font-size: 12px;"></div>
Also, see the button code
<asp:Button ID="btnSend" runat="server" ValidationGroup="VG"
OnClick="btnSend_Click" Class="button-form" Width="65" />
On button click event
protected void btnSend_Click(object sender, EventArgs e)
{
if (!checkcountry.Checked)
{
errordiv.InnerHtml = "Please select the authorization checkbox to proceed.";
return;
}
if (Page.IsValid)
{
// Response.Redirect("LegalPopup1.aspx");
ClientScript.RegisterClientScriptBlock(this.GetType(), "ResponseDialog", "$(document).ready(function(){ResponseDialog();});", true);
}
}
Add AutoPostBack as true for the DropDownList and add selected index changed event like below
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true"
inside your "ddlCountry_SelectedIndexChanged" code, you can check the selected values, checkbox values and do whatever you want based on those values.
Code
protected void btnSend_Click(object sender, EventArgs e)
{
if (checkcountry.Checked == true)
{
if (checkcountry.Checked == true && ddlCountry.SelectedIndex != 0 && ddlCountry.SelectedItem.Text == "India")
{
divForInida.Visible = true;
divForOther.Visible = false;
}
else if (checkcountry.Checked == true && ddlCountry.SelectedIndex != 0 && ddlCountry.SelectedItem.Text != "India")
{
divForInida.Visible = false;
divForOther.Visible = true;
}
else
{
//message for `Select a country of residence`
}
}
else
{
//message for `check the checkbox`
}
}
<asp:DropDownList ID="ddlCountry" runat="server" OnSelectedIndexChanged="ddlCountry_SelectedIndexChanged" AutoPostBack="true" >
And Your selected index changed event will look like this
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrEmpty(ddlCountry.SelectedItem.Text))
{
if (ddlCountry.SelectedItem.Text == "India")
{
}
else
{
}
}
}
catch (System.Exception ex)
{
throw new MyException(" Message:" + ex.Message, ex.InnerException);
}
}
Related
I should disable all the radio buttons belonging to a group
I have an asp.net page containing several checks. I should make sure that depending on the option selected inside a listbox, a certain group of radio buttons will be hide or disabled.
<asp:DropDownList ID="modelTypeSelectIN" runat="server" AutoPostBack="true" OnSelectedIndexChanged="modelTypeSelect_SelectedIndexChanged">
<asp:ListItem Value="" Selected="True"> SELEZIONA IL TIPO DI FINITURA</asp:ListItem>
<asp:ListItem Value="air_IN">AIR</asp:ListItem>
<asp:ListItem Value="wide_IN">WIDE</asp:ListItem>
<asp:ListItem Value="sound_IN">SOUND</asp:ListItem>
<asp:ListItem Value="wave_IN">WAVE</asp:ListItem>
<asp:ListItem Value="box_IN">BOX</asp:ListItem>
<asp:ListItem Value="plana_IN">PLANA</asp:ListItem>
<asp:ListItem Value="planaFG_IN">PLANA FULL GLASS</asp:ListItem>
</asp:DropDownList>
<div class="box" runat="server" id="BoxProva"><asp:RadioButton ID="sup3" GroupName="Superfici" runat="server" AutoPostBack="true" /><span><span class="img" runat="server"><img src="~/In/Superfici/AIR_SLIGHTGRAIN/AIR SLIGHTGRAIN_8016_IN LUCE.png" alt="" runat="server"></span>PROVA DI FILTRO</span><div class="image-box"><img src="~/In/Superfici/AIR_SLIGHTGRAIN/AIR SLIGHTGRAIN_8016_IN LUCE.png" alt="" runat="server"></div></div>
<div class="box" runat="server" id="BoxProva"><asp:RadioButton ID="sup4" GroupName="Superfici" runat="server" AutoPostBack="true" /><span><span class="img" runat="server"><img src="~/In/Superfici/AIR_SLIGHTGRAIN/AIR SLIGHTGRAIN_8017_IN LUCE.png" alt="" runat="server"></span>PROVA DI FILTRO</span><div class="image-box"><img src="~/In/Superfici/AIR_SLIGHTGRAIN/AIR SLIGHTGRAIN_8017_IN LUCE.png" alt="" runat="server"></div></div>
This is a event handler:
protected void SlightGrain7016InLuce_CheckedChanged(object sender,
EventArgs e)
{
if (SlightGrain7016InLuce.Checked)
{
BoxProva.Visible = false;
}
}
something like this will work , use function to managed your radio button group ...
// Thsi will trigger When Dropdownlist selected option changed
protected void modelTypeSelect_SelectedIndexChanged(object sender, EventArgs e)
{
if (modelTypeSelectIN.SelectedItem.Text == "WIDE")
{
Disable_GroupSuperfici_RaddioButton();
}
else
{
Enable_GroupSuperfici_RaddioButton();
}
}
// This function Enable particular group of button
private void Disable_GroupSuperfici_RaddioButton()
{
sup3.Enabled = false;
sup4.Enabled = false;
//RadioButton1.Enabled = false;
//RadioButton2.Enabled = false;
}
// This function disable particular group of button
private void Enable_GroupSuperfici_RaddioButton()
{
sup3.Enabled = true;
sup4.Enabled = true;
//RadioButton1.Enabled = true;
//RadioButton2.Enabled = true;
}
I have this code
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
RadioButton2.Checked = false;
DropDownList1.Enabled = true;
}
if (!RadioButton1.Checked)
{
RadioButton2.Checked = true;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
RadioButton1.Checked = false;
DropDownList1.Enabled = false;
}
if (!RadioButton1.Checked)
{
RadioButton1.Checked = true;
}
}
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="BookStore" DataTextField="Name" DataValueField="Name" Height="51px" Width="300px" DataMember="DefaultView">
</asp:DropDownList>
<asp:SqlDataSource ID="BookStore" runat="server" ConnectionString="<%$ ConnectionStrings:BookStoreConnectionString %>" SelectCommand="SELECT [Name] FROM [Books] ORDER BY [Name]"></asp:SqlDataSource>
<p>
<asp:RadioButton ID="RadioButton1" runat="server" Checked="True" GroupName="1" OnCheckedChanged="RadioButton1_CheckedChanged" />
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="1" OnCheckedChanged="RadioButton2_CheckedChanged" />
</p>
But when I click radiobuttons, dropdownlist is alwais enabled. When I write this functions in webforms app(not asp.net) its working correct.What could be the reason of this uncorrect working?
May be problem in RadioButton2_CheckedChanged event in second if condition, this should check RadioButton2.Checked not RadioButton1.Checked. instead of checking this condition if you put else still this will work no need to check again.
if (!RadioButton2.Checked)
{
RadioButton1.Checked = true;
}
I have created a simple radiobuttonlist. And I wrote simple function to do something on selection
here is the radiobuttonlist
<asp:RadioButtonList ID="payment_type" CssClass="rbl_type" runat="server" TextAlign="Right" RepeatDirection="Horizontal" BorderStyle="None" OnSelectedIndexChanged="payment_type_SelectedIndexChanged">
<asp:ListItem Value="0">Serbest Ödeme</asp:ListItem>
<asp:ListItem Value="1">Ön Tanımlı Ödeme</asp:ListItem>
</asp:RadioButtonList>
here is the c# code
protected void payment_type_SelectedIndexChanged(object sender, EventArgs e)
{
if (payment_type.SelectedValue == "0")
{
pnl_serbest.Visible = true;
pnl_on_tanimli.Visible = false;
}
else
{
pnl_serbest.Visible = false;
pnl_on_tanimli.Visible = true;
}
}
but it doesnt trigger anything. what am I doing wrong?
Add
AutoPostBack="true"
attribute to your <asp:RadioButtonList /> tag.
Following is the test scenario for my code.
1) Once the user selects one of the radio buttons on Webpage.aspx, a modal popup extender shows up.
2) A user control (SSL_Ticket.ascx) is defined inside the modal popup window.
3) A RequiredFieldValidator is defined for a drop down list contained inside the user control.
4) If the user selects the "0" value from drop down list, no validation error message is displayed.
Code
Webpage.aspx
<asp:RadioButtonList ID="RadioButtonListForTicket" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="radioButtonListForTicket_OnSelectedIndexChanged">
<asp:ListItem Selected="True">No</asp:ListItem>
<asp:ListItem>Yes</asp:ListItem>
</asp:RadioButtonList>
<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtenderForTicket" runat="server" BackgroundCssClass="popUpStyle"
DropShadow="true" PopupControlID="divTicketPreview" PopupDragHandleControlID="panelDragHandle"
TargetControlID="btnForPopupAppear" CancelControlID="btnForPopupDisappear"/>
....
...
Webpage.aspx.cs
protected void radioButtonListForTicket_OnSelectedIndexChanged(object sender, System.EventArgs e)
{
if (RadioButtonListForTicket.SelectedItem.Text.ToString().Equals("Yes"))
{
// Check if the sites are selected
updateSelectionCount();
updateListOfSites();
if (selectionCount == 0)
{
lblSSLTicketSelection.Text = "Please select a site.";
RadioButtonListForTicket.SelectedValue = "No";
return;
}
else
{
lblSSLTicketSelection.Text = "";
}
....
ModalPopupExtenderForTicket.Show();
}
}
...
SSL_Ticket.ascx
<asp:DropDownList ID="cmbRootCause" runat="server" Width="255px" OnSelectedIndexChanged="cmbRootCause_SelectedIndexChanged" AutoPostBack="true"
CausesValidation="true">
<asp:ListItem Value="0">Select</asp:ListItem>
<asp:ListItem Value="1">Item1</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select"
ErrorMessage="Please select root cause" ValidationGroup="validateRootCause" Visible="false" Display="Dynamic" EnableClientScript="true">
</asp:RequiredFieldValidator>
...
SSL_Ticket.ascx.cs
protected void cmbRootCause_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbRootCause.SelectedItem.ToString().Equals("Other"))
{
lblcmbRootCause.Text = "";
lblcmbRootCause.Visible = false;
txtRootCauseOther.Visible = true;
}
else if (cmbRootCause.SelectedItem.ToString().Equals("Select"))
{
lblcmbRootCause.Visible = true;
lblcmbRootCause.Text = "Please select root cause";
}
else
{
lblcmbRootCause.Text = "";
lblcmbRootCause.Visible = false;
txtRootCauseOther.Visible = false;
}
}
I did browse through couple of solutions (ValidateProperty, Client-side validation, RangeValidation, etc), but it did not fire validation text.
This did not help - Handling RequiredFieldValidator inside of a User Control
I'd appreciate your help very much.
Thanks!!!
Remove visible = false attribute from required field validator, by default they won't show up in the beginning.
<asp:RequiredFieldValidator ID="reqdFieldForRootCause" runat="server" ControlToValidate="cmbRootCause" InitialValue="Select"
ErrorMessage="Please select root cause" ValidationGroup="validateRootCause" **Visible="false"** Display="Dynamic" EnableClientScript="true">
</asp:RequiredFieldValidator>
Well in your 'RequiredFieldValidator' for your DropDownList you need to remove this:
InitialValue="Select"
I'm basically trying to bind the dropdown list with the radio buttons. there are 4 options in the dropdown menu according to which the radio buttons should be selected.
with the first two options, the radio buttons should be active and with the rest of two objects in the dropdown menu, the radio buttons should become inactive.
here is my front end code for dropdown:
<asp:DropDownList ID="ddLType" runat="server" Width="406px" Height="23px">
<asp:ListItem Value="Prof">Professional</asp:ListItem>
<asp:ListItem>Enterprise</asp:ListItem>
<asp:ListItem>Maintanence</asp:ListItem>
<asp:ListItem>Reporting</asp:ListItem>
</asp:DropDownList>
here is my code for radio buttons:
<asp:RadioButtonList ID="rdoMeapSupport" RepeatDirection="Horizontal" runat="server" AutoPostBack="True" >
<asp:ListItem Value="1" Text="Yes" />
<asp:ListItem Value="0" Text="No" />
</asp:RadioButtonList>
if we select professional, the radio buttons should be active with yes as the checked option.
with enterprise, both the buttons should be active but not selected.
with maintenance and reporting, the buttons should become inactive.
First of all you have to set the property of drop down list called AutoPostBack to true. You can do this by simply selecting your drop down list and set AutoPostBack = true from properties window.
Then go to code behind file write these codes:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
if (ddLType.SelectedValue == "Professional")
{
rdoMeapSupport.Enabled = true;
rdoMeapSupport.SelectedValue = "Yes";
}
}
}
after that set event for your radio button list "SelectedIndexChanged"
and paste this code inside that
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddLType.SelectedValue == "Professional")
{
rdoMeapSupport.Enabled = true;
rdoMeapSupport.SelectedValue = "Yes";
}
if (ddLType.SelectedValue == "Enterprise")
{
rdoMeapSupport.SelectedValue = null;
rdoMeapSupport.Enabled = true;
}
if ((ddLType.SelectedValue == "Maintanence") || (ddLType.SelectedValue == "Reporting"))
{
rdoMeapSupport.SelectedValue = null;
rdoMeapSupport.Enabled = false;
}
}
Apply AutoPostBack="true" attribute to the dropdown and do the below logic in the selected index change event.
<asp:DropDownList ID="ddLType" runat="server" Width="406px" Height="23px"
onselectedindexchanged="ddLType_SelectedIndexChanged" AutoPostBack="true" >
<asp:ListItem Value="Prof">Professional</asp:ListItem>
<asp:ListItem>Enterprise</asp:ListItem>
<asp:ListItem>Maintanence</asp:ListItem>
<asp:ListItem>Reporting</asp:ListItem>
</asp:DropDownList>
<asp:RadioButtonList ID="rdoMeapSupport" RepeatDirection="Horizontal" runat="server"
AutoPostBack="True">
<asp:ListItem Value="1" Text="Yes" />
<asp:ListItem Value="0" Text="No" />
</asp:RadioButtonList>
protected void ddLType_SelectedIndexChanged(object sender, EventArgs e)
{
if (ddLType.SelectedIndex == 0 || ddLType.SelectedIndex == 1)
{
rdoMeapSupport.Enabled = true;
}
else
{ rdoMeapSupport.Enabled = false; }
}
I think making radio buttons active, but not allowing to select is none of use, better you disable it.
You may use jquery for this:
<script>
$(function () {
$("# <%# ddLType.ClientID %>").change(function () {
var selVal = $(this).val();
if(selVal == "Prof"){
$('#rb_Statusno').removeAttr('disabled');
else
$('#rb_Statusno').attr('disabled', true);
}
</script>
For more help you may go through this post.