I have 2 radio buttons and a drop down list under option 1 and option 2. the user can either select the radio button or the drop down list.
option 1
radiobtn1
radiobtn2
option2
dropdownlistbox
the radiobtn1 under option1 is checked as default. When the user select the value from the dropdown list, both the radio buttons should be disabled or it should deselects the radiobtn1 which is checked as default.
<tr><td>option1</td></tr>
<tr>
<td><asp:RadioButton ID="rdo" GroupName="Month" Text="radiobtn1" runat="server /></td>
<td><asp:RadioButton ID="rdo2" GroupName="Month" Text="radiobtn2" runat="server /></td>
</tr>
<tr><td>option2</td></tr>
<tr><td>
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="ddMonYear_SelectedIndexChanged">
<asp:ListItem Text="opt1"></asp:ListItem>
<asp:ListItem Text="opt2"></asp:ListItem>
</asp:DropDownList>
</td></tr>
protected void Page_Load(object sender, EventArgs e)
{
rdo.Checked=true;
if (ddMonYear.SelectedValue.Length.ToString() != "0")
{
rdo.Checked = false;
rdo.Enabled = false;
}
else
{
rdo.Checked = true;
}
}
The above code doesnt work. the radiobtn 1 is not deselecting when i select the value from the drop down.
Help me in correcting my code.
Thanks.
On your dropdown box, add this code (autopostback)
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="ddMonYear_SelectedIndexChanged" AutoPostBack=True">
What this does is keep the value of the dropdown box. Each time your page is loaded, the dropdown box gets the default value before your pageload event fires. give that a try.
Related
In my aspx file, I have a drop down list
<asp:DropDownList runat="server" ID="Dlist" CssClass="dropdown" AutoPostBack="true" SelectedIndexChanged="CtrlChanged">
<asp:ListItem Text="Select item" Value="1"></asp:ListItem>
</asp:DropDownList>
I have a radio button list
<asp:RadioButtonList ID="RadioButtonList1" RepeatColumns="1"
RepeatDirection="Vertical" RepeatLayout="Table" runat="server" AutoPostBack="true">
<asp:ListItem>Option 1</asp:ListItem>
<asp:ListItem>Option 2</asp:ListItem>
</asp:RadioButtonList>
Now I want to change the name of one or both of the radio buttons in the radio button list after something has been selected from the dropdown list using C#. Below is my attempt but not working.
protected void CtrlChanged(Object sender, EventArgs e) {
//attempting to change text of first radio button when item has been selected from dropdownlist
RadioButtonList1.SelectedIndex = 0;
RadioButtonList1.SelectedItem.Text = "Text changed!";
}
First, it is OnSelectedIndexChanged, not SelectedIndexChanged. And the ListItems of a RadioButtonList are index based, so you need to access them like this:
protected void CtrlChanged(object sender, EventArgs e)
{
RadioButtonList1.Items[0].Text = "NewValue 1";
RadioButtonList1.Items[1].Text = "NewValue 2";
}
Your way does change the text, but only for the item you set the SelectedIndex of. And it will change the selected radiobutton to the first one, should one already have been selected.
I have two checkboxes a true and a false. The user should only be allowed to select one, when one is selected the other is deselected and vice versa. I have an on change event which does this, but If i select one so the the other deselects and then press the browser back button they both appear selected?! I put in a method to check for this event, but when they both appeared select on the front end the logic in the back end was seeing one as being unselected? Does this make sense?
My Check boxes:
<asp:PlaceHolder runat="server" ID="phIsValidated">
<asp:CheckBox ID="chbTrue"
Text="True"
runat="server"
AutoPostBack="True"
OnCheckedChanged="Check_Clicked" />
<asp:CheckBox ID="chbFalse"
Text="False"
runat="server"
AutoPostBack="True"
OnCheckedChanged="Check_Clicked" />
</asp:PlaceHolder>
My on changed event:
protected void Check_Clicked(Object sender, EventArgs e)
{
CheckBox checkBox = (CheckBox)sender;
if (checkBox.ID=="chbTrue")
{
chbFalse.Checked = !chbTrue.Checked;
}
else
{
chbTrue.Checked = !chbFalse.Checked;
}
}
<asp:RadioButtonList ID="MyRadioButtons" runat="server">
<asp:ListItem Value="Y">Yes</asp:ListItem>
<asp:ListItem Value="N">No</asp:ListItem>
</asp:RadioButtonList>
Here is an example with code to process the selected item:
http://www.w3schools.com/aspnet/showaspx.asp?filename=demo_radiobuttonlist
Step 1: Add two Checkboxes.
Step 2: Add OnCheckedChanged Event to all the checkboxes.
(In case if you do not know How to Add OnChecKedChanged Event, You can follow these steps.
There are two ways to Add this.
1st You can directly add It to your code like
<asp:CheckBox runat="server" ID="Try1ID" OnCheckedChanged="Try1ID_CheckedChanged"/>
Or
2nd You can open the design tab in visual studio and double click on the checkbox you want to create OnChecKedChanged event.
)
Step 3: Set AutoPostBack = True.
(
Example :
<asp:CheckBox runat="server" ID="Try1ID" AutoPostBack="True" OnCheckedChanged="Try1ID_CheckedChanged"/>
or
you can set this from checkbox property window.
)
Step 4 : Add If Else Condition in your .cs file inside the OnChecKedChanged Event
(
Example: If You have two checkboxes with names Try1ID_CheckedChanged and Try2ID_CheckedChanged
Your condition should look like this,
protected void Try1ID_CheckedChanged(object sender, EventArgs e)
{
if (Try1ID.Checked)
{
Try2ID.Checked = false;
}
}
protected void Try2ID_CheckedChanged(object sender, EventArgs e)
{
if (Try2ID.Checked)
{
Try1ID.Checked = false;
}
}
)
Step 5: You Press F5 and test the program.
I need to add a condition to a DropDownList where a method can be executed by button click only if the user has selected a value different than the listItem (default value).
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource5"
DataTextField="proj_name" DataValueField="proj_name">
<asp:ListItem Text="Select a project to clone" Value="" />
</asp:DropDownList>
How can I structure an if condition to validate that the selected value is not the ListItem (default value)?
You can use asp.net delivered validation controls
Ex:
<asp:RequiredFieldValidator id="rfv1"
ControlToValidate="DropDownList1"
Display="Static"
ErrorMessage="* Select a value"
InitialValue="DefaultValueHere"
runat="server"
ValidationGroup="V1"/>
Then edit your button markup to use ValidationGroup
<asp:Button Id="button1" ValidationGroup="V1" .../>
In your codebehind button click code add this
protected void button1_onlick(Object sender, EventArgs e)
{
If(Page.IsValid)
{
// your existing code here
}
}
See sample code below
if (DropDownList1.SelectValue == "")
{
// Write your code here
}
you can also have:
if (DropDownList1.Text == "Select a project to clone")
{
// Write your code here
}
I added a DropDownList from the Toolbox to the Login page on a website I'm working on.
Once I choose a ListItem in the DropDownList (in my case lets say
Gym for example...), when clicked, I want that three bars we'll be opened below my DropDownList(for example, the bars that we'll be opened are Username, Password and ID), I mean three TextBoxes beneath each other.
I think you can either try the SelectedIndexChanged event or Javascript to display the textboxes without a postback.
<select>
<option value="1">Gym 1</option>
<option value="2">Gym 2</option>
<option value="3">Gym 3</option>
<select>
At firest you put your textboxes in a panle then hide this panel and
you should set Autopostback property of you drobdownlist to True and after selecting a item in DropDownList, postback will accur. So you can show that panel include text boxes.
What you might really be needing is dynamic text boxes.
In the html part:
<asp:DropDownList runat="server" ID="DDL1" autopostback = "true" onselectedindexchanged="DDL_SelectChanged" />
<asp:PlaceHolder runat="server" ID="PH1">
</asp:PlaceHolder>
In codebehind:
void DDL_SelectChanged(object sender, EventArgs e)
{
if (DDL1.SelectedIndex == 1)
{
for (int i = 0; i < 3; i++)
{
TextBox newTB = new TextBox();
newTB.ID = "TB" + i;
PH1.Controls.Add(newTB);
}
}
}
You could use MultiveView control. And set active view index in Dropdown's selectedIndexChanged event.
I wrote some example code for you:
ASPx side:
<asp:MultiView ID="multiView" ActiveViewIndex="-1" runat="server">
<asp:View ID="viewGym" runat="server">
<asp:TextBox ID="txtBxUserName" runat="server" />
<asp:TextBox ID="txtBxPassword" runat="server" />
<asp:TextBox ID="txtBxId" runat="server" />
</asp:View>
</asp:MultiView>
<asp:DropDownList ID="Dropdownlist1" runat="server" AutoPostBack="true"
onselectedindexchanged="Dropdownlist1_SelectedIndexChanged">
<asp:ListItem Text="Choose one club" Value="0" />
<asp:ListItem Text="Gym" Value="1" />
<asp:ListItem Text="Shoppers" Value="2" />
</asp:DropDownList>
Code Behind:
protected void Page_Load(object sender, EventArgs e)
{
if ( IsPostBack ) //don't forget :)
return;
}
protected void Dropdownlist1_SelectedIndexChanged( object sender, EventArgs e )
{
if ( Dropdownlist1.SelectedValue == "1" ) //Gym item selected
{
multiView.ActiveViewIndex = 0; //Gym view active
}
}
If you want to any view not active when first page load then you set the ActiveViewIndex with -1 in aspx code.
There's this repeater...
<asp:Repeater ID="myRepeater" OnItemCommand="rpt1_ItemCommand" runat="server" OnItemDataBound="rpt1_OnItemDataBound">
<HeaderTemplate>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tr class="lgrey">
<td>Default</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:LinkButton ID="lnk1" Text="Make Default" CommandName="SetDefault" runat="server" Visible="True" CommandArgument='<%#Eval("UserID") %>' CausesValidation="false"></asp:LinkButton>
<asp:Label ID="label1" Text="Yes" runat="server" Visible="False"></asp:Label>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
What I want is that when user clicks on any of the "lnk1" link button in the lsit that repeater renders,
the link should be replaced with the label "label1"..ie when the user clicks on "Make Default" link, it should be replaced
with "Yes" label
Now when I click 2 link buttons, both get their label "Yes" displayed where as I want only one link button to display Yes
ie the one which has been clciked and rest of the items should display "Make Default" link button only.
ie Only ONE item should be displaying "Yes" label...now how do I iterate through the repeater items to set only ONE item
as default and not multiple ??
You can iterate the repeater items collection,
protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
int selectedIndex = e.Item.ItemIndex;
foreach(RepeaterItem item in myRepeater.Items)
{
((LinkButton)item.FindControl("lnk1")).Visible = (item.ItemIndex != selectedIndex);
((Label)item.FindControl("label1")).Visible = (item.ItemIndex == selectedIndex);
}
}
The pros of this option are: 1. no second hit on the database.
Or I would put my logic in the ItemDataBound event instead, store the clicked link button index in a member variable and call DataBind in the command event handler.
private int selectedIndex = -1;
//...
protected void myRepeater_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
selectedIndex = e.Item.ItemIndex;
myRepeater.DataSource = MyGetDataMethod();
myRepeater.DataBind();
}
In the ItemDataBound handler compare the current index with the stored index and if they match show the label.
protected void myRepeater_ItemDataBound(Object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if(e.Item.ItemIndex == selectedIndex)
{
((LinkButton)e.Item.FindControl("lnk1")).Visible = false;
((Label)e.Item.FindControl("label1")).Visible = true;
}
}
}
The cons of this second option are: 1. A second hit on the database. 2. If the user clicks say row two, and some other user inserts a new address record, row 2 may now be something different when you re-bind. Also if you're not using an order by that could change between database calls and your stored selectedIndex could be invalidated thatway too.
So in conclusion I'd go with option one now I've thought it all the way through.