I have a DropDownList and a TextBox in c#.net. If DropDownList value is "No" then some value must enter in TextBox. If dropdownlist value is yes then required field validator is not needed for that TextBox. How to make it possible?
<asp:DropDownList ID="dropdownlist1" runat="server"
CssClass="NormalText" Width="155px" AutoPostBack="true"
onselectedindexchanged="ddls_SelectedIndexChanged">
<asp:ListItem Selected="True" Value=""></asp:ListItem>
<asp:ListItem Value="Yes">Yes</asp:ListItem>
<asp:ListItem Value="No">No</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:TextBox ID="Textbox1" runat="server" CausesValidation="True"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Textbox1" ErrorMessage="Explanation needed If you select NO">
</asp:RequiredFieldValidator>
</td>
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Use this code for conditional validation:
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
if(dropdownlist1.SelectedValue == "No")
{
RequiredFieldValidator1.Enabled = true;
}
else if(dropdownlist1.SelectedValue == "Yes")
{
RequiredFieldValidator1.Enabled = false;
}
}
Optionally, you may also have to set Submit Button Validation group:
ButtonSubmit.ValidationGroup = string.Empty;
when field validator is not enabled.
Related
So, I have these elements, label and drop-down list I want to control their visibility according to the selected value in other drop-down list
the main drop-down list is DropDownList1
<asp:DropDownList ID="DropDownList1" runat="server" onselectedindexchanged="DropDownList1_SelectedIndexChanged"
CssClass="dropdownRequestList">
<asp:ListItem>Teacher</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>
and other element need to control their visibility Label9 and DropDownList2
<asp:Label ID="Label9" runat="server" Text="Department :-" CssClass="lable"
Visible="True"></asp:Label>
</td>
<td class="tx">
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Department"
DataValueField="Department" Visible="True" AutoPostBack="True">
</asp:DropDownList>
then I write c# function to control the visiblity
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "Teacher")
{
Label9.Visible = false;
DropDownList2.Visible = false;
}
}
But those elements still visible when select Admin, I also try to initialize visibility for both elements as false then make them true when Teacher value selected but no things appear in page and they still hidden, What is the wrong here ?
Enable post back on for DropDownList1 so your drop down can call the c# server-side code.
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
CssClass="dropdownRequestList" AutoPostBack="True">
<asp:ListItem>Teacher</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
</asp:DropDownList>
And for c# code below update the visibility on Page_Load instead of DropDownList1_SelectedIndexChanged to handle all cases as below.
protected void Page_Load(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text != "Teacher")
{
Label9.Visible = false;
DropDownList2.Visible = false;
}
else
{
Label9.Visible = true;
DropDownList2.Visible = true;
}
}
Add AutoPostBack="True" to DropDownList1:
<asp:DropDownList ID="DropDownList1" runat="server"
onselectedindexchanged="DropDownList1_SelectedIndexChanged"
CssClass="dropdownRequestList" AutoPostBack="True">
I am trying to disable a RequiredFieldValidator by radio button list value. I have it coded but it doesn't seems to be working.
What i have done so far:
protected void radioButtonList(object sender, EventArgs e)
{
if (((RadioButtonList)dvInsertPromotion.FindControl("RadioButtonList2")).SelectedValue == "Y")
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = false;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = false;
this.addPromotion.Show();
}
else
{
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Enabled = true;
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate")).Enabled = true;
this.addPromotion.Show();
}
}
html:
<asp:RadioButtonList ID="RadioButtonList2" runat="server" ValidationGroup="addValidationGp" OnSelectedIndexChanged="radioButtonList">
<asp:ListItem Text="Yes" Value="Y"></asp:ListItem>
<asp:ListItem Text="No" Value="N" Selected></asp:ListItem>
</asp:RadioButtonList>
<asp:TextBox ID="txtPubDate" Width="75" MaxLength="10" runat="server" AutoPostBack="true" OnTextChanged="insertStartEndDateTime_SelectedIndexChanged"/>
<asp:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="txtPubDate"
PopupPosition="Right" Format="dd/MM/yyyy" />
<asp:RequiredFieldValidator ID="rfvdate" ValidationGroup="addValidationGp" runat="server"
ControlToValidate="txtPubDate" ErrorMessage="*" Display="Dynamic" Enabled="true"
SetFocusOnError="true" /><br />
I've had the same problem in the past and the only way I was able to solve this was by making the validators invisible altogether:
((RequiredFieldValidator)dvInsertPromotion.FindControl("rfvdate1")).Visible = false;
Just don't forget to set them to visible again whenever you want to enable them.
I have a repeater with a RadioButtonList and TextBox inside. I want the TextBox to be required only if the RadioButtonList has a value of 1 or 2. How can I achieve this?
I thought I could initially enable the RFV on a selected index change but it can't see the items since it is within a repeater. Do I need to do this within the ItemDataBound?
<asp:Repeater ID="repeaterSurvey" runat="server">
<ItemTemplate>
<div class="form-group">
<asp:Label ID="labelQuestion" runat="server" Text='<%# Eval("Question")%>' />
<asp:RequiredFieldValidator ID="RFV1" CssClass="required" runat="server"
ErrorMessage="Required" Display="Dynamic" ControlToValidate="surveyList" />
<asp:RadioButtonList RepeatDirection="Horizontal" ID="surveyList" AutoPostBack="true"
OnSelectedIndexChanged="surveyList_SelectedIndexChanged" runat="server">
<asp:ListItem Value="1">Strongly Disagree</asp:ListItem>
<asp:ListItem Value="2">Disagree</asp:ListItem>
<asp:ListItem Value="3">Agree</asp:ListItem>
<asp:ListItem Value="4">Strongly Agree</asp:ListItem>
<asp:ListItem Value="0">N/A</asp:ListItem>
</asp:RadioButtonList>
<asp:HiddenField ID="hiddenfieldID" Value='<%# Eval("ID")%>' runat="server" />
<asp:TextBox ID="textboxComment" Placeholder="Comments" CssClass="form-control" TextMode="MultiLine"
Rows="3" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RFV2" Enabled="false" CssClass="required" runat="server"
ErrorMessage="Comment Required" Display="Dynamic" ControlToValidate="textboxComment" />
</div>
</ItemTemplate>
</asp:Repeater>
Codebehind
protected void surveyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (surveyList.SelectedValue == "1" || surveyList.SelectedValue == "2")
{
RFV2.Enabled = true;
}
else
{
RFV2.Enabled = false;
}
}
With the help of the comment section (stephen.vakil) I was able to create a solution to my problem:
protected void surveyList_SelectedIndexChanged(object sender, System.EventArgs e)
{
RadioButtonList surveyList = (RadioButtonList)sender;
string value = surveyList.SelectedValue;
RepeaterItem row = (RepeaterItem)surveyList.NamingContainer;
if (value == "1" || value == "2")
{
((RequiredFieldValidator)row.FindControl("RFV2")).Enabled = true;
}
else
{
((RequiredFieldValidator)row.FindControl("RFV2")).Enabled = false;
}
}
I want to change the DropDownList1 value if Checkbox1 is checked in my Web Application.
The Default Value on the DropDownList1 when the page loads should be "0"
When the Checkbox1 is clicked, the DropDownList1 value should be "1"
The following code which I have done throws a runtime error.
My aspx snippet:
<asp:CheckBox ID="Checkbox1" runat="server"
class="itemCheck" AutoPostBack="True" CausesValidation="True" />
</td>
<td >
<asp:DropDownList ID="DropDownList1" runat="server" Height="16px"
Width="138px">
<asp:ListItem Selected="True">0</asp:ListItem>
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
My code behind file:
protected void Page_Load(object sender, EventArgs e)
{
string val = "1";
ListItem checkItem = DropDownList1.Items.FindByValue(val);
if (checkItem != null)
{
DropDownList1.ClearSelection();
checkItem.Selected = true;
}
}
Your listitems are missing a value attribute.
<asp:ListItem Value="1">1</asp:ListItem>
Remove your Page_Load code and replace checkbox markup with below mark up
<asp:CheckBox ID="Checkbox1" runat="server" class="itemCheck" AutoPostBack="True" CausesValidation="True" OnCheckedChanged="Checkbox1_CheckedChanged"/>
and write your logic in this event
protected void Checkbox1_CheckedChanged(object sender, EventArgs e)
{
If (Checkbox1.Checked) DropDownList1.SelectedValue = "1";
else DropDownList1.SelectedValue = "0";
}
i have 4 dropdownlist. when i change the 1st dropdown, onselectindexchanged i need to change all the remaining dropdown values.
I am working on it but i could change only the corresponding or next dropdown value on onselectindexchanged. Please provide me any example or guide me to any good link.
Pls help.. Thanks in advance.
You need to set AutoPostBack of all dropdowns to true and need to add SelectedIndexChanged event for first three drop downs. On SelectedIndexChanged of first dropdown you need to set selected value of second and it will fire SelectedIndexChanged of second and there you will have code to set selected index of third and similarly you will do on wards.
In Html
<asp:DropDownList ID="dropdownlist1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdownlist1_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dropdownlist2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdownlist2_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dropdownlist3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="dropdownlist3_SelectedIndexChanged">
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="dropdownlist4" runat="server" AutoPostBack="true" >
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:DropDownList>
1
2
In code behind
protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
{
dropdownlist2.SelectedIndex = someIndexValue;
}
protected void dropdownlist2_SelectedIndexChanged(object sender, EventArgs e)
{
dropdownlist3.SelectedIndex = someIndexValue;
}
protected void dropdownlist3_SelectedIndexChanged(object sender, EventArgs e)
{
dropdownlist4.SelectedIndex = someIndexValue;
}