I have a simple windows form app which I have to migrate to run on a webpage, so I'm trying with aspx(c#).
I have two radio buttons, but at time only one of them should be checked. I implemented the very same code from the original app, but it's not working:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked == true)
{
RadioButton2.Checked = false;
}
}
protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton2.Checked == true)
{
RadioButton1.Checked = false;
}
}
So why these changes not being applied on the page?
you can use GroupName Property of RadioButton Control.
if you set the same GroupName for set of RadioButtons you don't need to write the Code Behid which you have written in the above Post/Question, as only one radio button can be selected from the group.
but if you want to invoke some action like Disabling TextBox on Particular Radio Button click event you can use following sample code.
Example: in this example i'm taking 3 Radio Buttons all set to Same GroupName, hence only one RadioButton canbe selected at a time.
when user selects/checks a RadioButton1 i'm Disabling the TextBox1.
Note : Please Make sure that your RadioButton AutoPostBack Property set to True otherwise events on RadioButton won't be fired.
Design Code:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:RadioButton ID="RadioButton1" runat="server" Text="DisableTextBox" GroupName="Group1" AutoPostBack="True" OnCheckedChanged="RadioButton1_CheckedChanged"/>
<asp:RadioButton ID="RadioButton2" runat="server" GroupName="Group1" AutoPostBack="True"/>
<asp:RadioButton ID="RadioButton3" runat="server" GroupName="Group1" AutoPostBack="True" />
Code Behind:
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
TextBox3.Enabled = false;
else
TextBox3.Enabled = true;
}
Put the checkboxes in a GroubBox or Panel
You can set the GroupName of both radiobuttons the same, so the Code Behind you wrote is not needed anymore.
Also, check out the RadioButtonList
UPDATE
Your aspx-code should look something like this:
<asp:RadioButton id="RadioButtonEnabled" runat="server" Text="Enabled" GroupName="GroupTxtEnabled" AutoPostBack="True" OnCheckedChanged="RadioButtonEnabled_CheckedChanged"/>
<asp:RadioButton id="RadioButtonDisabled" runat="server" Text="Disabled" GroupName="GroupTxtEnabled" AutoPostBack="True"/>
<asp:TextBox id="TextBox1" runat="server"/>
And in your code-behind:
protected void RadioButtonEnabled_CheckedChanged (object sender, EventArgs e)
{
TextBox1 = RadioButtonEnabled.Checked
}
As you can see, I've used the OnCheckedChanged only at one radiobutton, since both radiobuttons are changed at the same time (property GroupName is equal).
Related
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 am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}
I have an AjaxControlToolkit ComboBox as follows:
<ATC:ComboBox
id="cmbid1"
runat="server"
Width="150px"
RenderMode="Block"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend">
</ATC:ComboBox>
Elsewhere on the page, I have a checkbox that I want to use to toggle the ComboBox:
<asp:CheckBox
ID="lockCheckBox"
runat="server"
Text="Locked"
Enabled="False"
OnCheckedChanged="lockCheckBox_CheckedChanged"
AutoPostBack="True" />
Then I have a handler like this:
protected void lockCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (lockCheckBox.Checked)
{
cmbid1.Enabled = false;
EnableControlNotLockedDown();
}
else
{
cmbid1.Enabled = true;
}
}
Instead of toggling the ComboBox from enabled to disabled and vice versa, the ComoBox just gets disabled and never becomes enabled again.
What am I doing wrong?
I have an asp.net page and on it there are a number of controls, including:
A textbox with autopostback = true and the server side textchanged event implemented
A button with the server side click event implemented
The textbox performs the postback as soon as the user leaves the control (ie, focus is lost). The problem is that if the user happens to change the value and then press the button without leaving the textbox, then on button click the textbox will perform the postback but the button click will be lost.
How can i force both the textbox and the button events to fire consecutively in such cases?
Thanks
Try:
ASPX:
<asp:TextBox ID="TextBox1" clientidmode="Static" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
JS:
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
CS:
protected void Button1_Click1(object sender, EventArgs e)
{
}
You can delay postback from textbox and cancel it in case of button click. To do this add code below to Page_PreRender method:
protected void Page_PreRender(object sender, EventArgs e)
{
Button1.OnClientClick = string.Format("if(window.{0}Timeout){{ clearTimeout(window.{0}Timeout); }}",
TextBox1.ClientID);
TextBox1.Attributes["onChange"] = string.Format("javascript:window.{0}Timeout = setTimeout(\"{1}\", 500); return;",
TextBox1.ClientID,
ClientScript.GetPostBackEventReference(TextBox1, ""));
}
I have 2 labels and 2 text boxes and 1 buttons displayed.
When the page loads the Name and Button (will be initially displayed). Later when i click on the Button i need to display the age label and textbox. How can i do this ?
<ol>
<li>
<asp:Label runat="server" AssociatedControlID="Name">
User name
</asp:Label>
<asp:TextBox runat="server" ID="Name" Width="167px" />
<asp:Button ID="Button1" runat="server" Text="Button" />
</li>
<li>
<asp:Label runat="server" AssociatedControlID="age">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
</ol>
code for button press
protected void Button1_Click(object sender, EventArgs e)
{
}
You could set the label/textbox Visible property to True in server side. Alternatively, you could use JavaScript to avoid post backs to the server.
Add OnClientClick to your button :
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="ShowLabel();"/>
and declare the JavaScript function on page:
<script type="text/javascript">
function ShowLabel() {
// Note that the client ID might be different from the server side ID
document.getElementById('lblAge').style.display = 'inherit';
}
</script>
You need to set the Label Display style to none initially.
<asp:Label ID="lblAge" style="display: none;" runat="server" AssociatedControlID="age">age</asp:Label>
Try below code:
You need to set Visible property of controls to True or False according to your requirement. By default, all controls are visible on the screen whenever they are added on the page.You need to do following thing:
You need to remove TextMode="age" as there is not any supported textmode of type age.
Need to define id of control if you want to access a control server side in code behind. So define the ID of Label that you put corresponding to Age textbox.
By Default age label and textbox will not be visible by using below code:
<asp:Label ID="lblAge" runat="server" AssociatedControlID="age" Visible="false">age</asp:Label>
<asp:TextBox runat="server" ID="age" Width="240px" Visible="false"/>
Code behind:
After button click age label and the textbox will be visible by using below code:
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible = true;
age.Visible = true;
}
First add id to elements and set visible false
<asp:Label runat="server" AssociatedControlID="age" Visible="false" Id="lbl1">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" Visible="false" />
button click event set visible true
protected void Button1_Click(object sender, EventArgs e)
{
lbl1.Visible = True;
age.Visible = True;
}
this is the basic concept of asp.net. you can use visible property of the control.
your TextMode enumeration is wrong. there is no Age enumeration for Textbox.TextMode TextMode
<li>
<asp:Label runat="server" AssociatedControlID="age" id="lblAge">age</asp:Label>
<asp:TextBox runat="server" ID="age" TextMode="age" Width="240px" />
</li>
in code behind
protected void Button1_Click(object sender, EventArgs e)
{
lblAge.Visible=true;
age.Visible=true;
}
protected void Page_Load(object sender, EventArgs e)
{
NameLabel.Visible = false;
NameBox.Visible = false;
}
protected void Button1_Click(object sender, EventArgs e)
{
NameLabel.Visible = true;
NameBox.Visible = true;
}