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">
Related
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.
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 2 RadioButtonLists and I want only one option to be selected, f.i. if you select an option in List 1 the selection in List 2 should be cleared.
AutoPostBack and EnableViewState is set to true, but still the Method never fires. I have also checked if the Index actually changes, it does. I think that the PostBack just doesn't occur, but I don'T know why.
I'm thankful for any help.
ascx:
<asp:RadioButtonList ID="_listOne" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListOneIndexChanged">
</asp:RadioButtonList>
<asp:RadioButtonList ID="_listTwo" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ListTwoIndexChanged">
</asp:RadioButtonList>
Code Behind:
protected void ListOneIndexChanged(object sender, EventArgs e)
{
_listTwo.ClearSelection();
}
protected void ListTwoIndexChanged(object sender, EventArgs e)
{
_listOne.ClearSelection();
}
change the control to RadioButton and add GroupName to them will do the trick:
<asp:RadioButton ID="_listOne" runat="server" GroupName="xx" AutoPostBack="true" OnSelectedIndexChanged="ListOneIndexChanged">
</asp:RadioButton>
<asp:RadioButton ID="_listTwo" runat="server" GroupName="xx" AutoPostBack="true" OnSelectedIndexChanged="ListTwoIndexChanged">
</asp:RadioButton>
try to use just one radibuttonlist and options.
<asp:RadioButtonList id="radiolist1" runat="server" OnSelectedIndexChanged="ListOneIndexChanged">
<asp:ListItem selected="true">Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
</asp:RadioButtonList>
When I perform a radiobuttonclick, I want to set a dropdownlist to become visible. The radiobutton and dropdownlist are within the same datagrid. I am not sure how to do this.
<asp:UpdatePanel ID="updatepanel" UpdateMode="conditional" runat="server">
<ContentTemplate>
<asp:DataGrid ID="DataGrid" AutoGenerateColumns = "false" CssClass="objectSubTitle" ItemStyle-Wrap="true" runat="server" OnItemCommand="handler" ><Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:RadioButton ID ="RadioButton1" Text="Yes" GroupName="creatingNewCard" OnCheckedChanged="RadioButtonYes" AutoPostBack="True" runat="server" />
<asp:DropDownList ID="DropDownList1" Visible="false" runat="server"/>
</ItemTemplate>
</asp:TemplateColumn>
Assuming that they are in an ItemTemplate of a TemplateField and you want to switch vivibility on serverside:
protected void RbCheckedChanged(Object sender, EventArgs e)
{
var radioButton1 = (RadioButton)sender;
var row = (GridViewRow)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)row.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked;
}
Sample-GridView:
<asp:GridView ID="GridView1" AutoGenerateColumns="false" OnRowDataBound="Grid_RowDataBound"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:RadioButton ID="RadioButton1" runat="server" OnCheckedChanged="RbCheckedChanged" AutoPostBack="true"></asp:RadioButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Edit: as you've edited your question to show that you really use a DataGrid instead of a GridView, the code is similar:
protected void RbCheckedChanged(Object sender, EventArgs e)
{
var radioButton1 = (RadioButton)sender;
var item = (DataGridItem)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked;
}
You can call the Visible property of the DropdwonList in the radio buttons checked changed event like this
protected void RadioButton1_CheckedChanged(Object sender, EventArgs e)
{
var radioButton1= (RadioButton)sender;
var item = (DataGridItem)radioButton1.NamingContainer;
var dropDownList1 = (DropDownList)item.FindControl("DropDownList1");
dropDownList1.Visible = radioButton1.Checked ? true : false;
}