RadioButton value to be changed everytime a linkbutton is clicked - c#

I have 5 radio buttons and a link in my page. Everytime when the linkbutton is clicked, i want my radiobutton to be changed to other. I mean, when a link is clicked, radiobutton check has to move onto rd2 from rd1. Is that possible.
Below is my piece of code for link button and radiobutons.
protected void lnkAddLoc_Click(object sender, EventArgs e)
{
}
<asp:RadioButton ID="rdoLoc1" runat="server" Text="None" TextAlign="left" GroupName="rdoLocation" Checked="true" Width="68px" OnCheckedChanged="rdoLoc1_CheckedChanged" Visible = "true"/>
<asp:RadioButton ID="rdoLoc2" runat="server" Text="1" TextAlign="Left" GroupName="rdoLocation" OnCheckedChanged="rdoLoc2_CheckedChanged" Width="68px" Visible = "true" />
<asp:RadioButton ID="rdoLoc3" runat="server" Text="2" TextAlign="Left" GroupName="rdoLocation" Width="68px" Visible = "true" />
<asp:RadioButton ID="rdoLoc4" runat="server" Text="3" TextAlign="Left" GroupName="rdoLocation" Width="66px" Visible = "true"/>
<asp:RadioButton ID="rdoLoc5" runat="server" Text="4" TextAlign="Left" GroupName="rdoLocation" Width="62px" Visible = "true"/>

protected void lnkAddLoc_Click(object sender, EventArgs e)
{
if (rdoLoc1.Checked)
rdoLoc5.Checked = true;
else if (rdoLoc2.Checked)
rdoLoc1.Checked = true;
else if (rdoLoc3.Checked)
rdoLoc2.Checked = true;
else if (rdoLoc4.Checked)
rdoLoc3.Checked = true;
else if (rdoLoc5.Checked)
rdoLoc4.Checked = true;
}

I'd seriously consider doing this client-side in javascript... a postback everytime you click a link to bump the radio button down? Your users will be extremely annoyed.

Related

ItemCommand not working with repeater and few events

I get a repeater with 3 radio button (grouped in 3 rows by GroupName property) and triggered (per each radio button). My problem is coming when I add a HiddenField to the repeater column and I want to reach this HiddenField through ItemCommand event.
The ItemCommand event is not raising...
(LinkButton click event is working as well)
(If a set CheckedChanged event of radio button they are working fine, but They are not helping because I need to reach the HiddenField)
This my code:
CSHTML
<asp:Repeater runat="server" ID="repeaterImages" OnItemDataBound="repeaterImages_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="lnkEliminarImagen" runat="server" OnClick="lnkEliminarImagen_Click" CausesValidation="false"><i class="fa fa-times" ></i></asp:LinkButton>
<span>
<asp:RadioButton runat="server" ID="rbLogoSeleccionado" Text='Logo 0' GroupName="nombreLogo" /><br />
<asp:RadioButton runat="server" ID="rbLogoSeleccionadoApp" Text='Logo 1' GroupName="nombreLogoApp" /><br />
<asp:RadioButton runat="server" ID="rbLogoSeleccionadoAppBlanco" Text='Logo 2' GroupName="nombreLogoAppBlanco" />
<asp:HiddenField runat="server" ID="hdLogoId" Value='<%#Eval("id").ToString()%>' />
</span>
</ItemTemplate>
</asp:Repeater>
JS
for simulating GroupName behaviour. Avoiding to have more than one
radio button checked per row
<script>
function SetUniqueRadioButton(text, current) {
for (i = 0; i < document.forms[0].elements.length; i++) {
elm = document.forms[0].elements[i]
if ((elm.type == 'radio') && (elm.value == text)) {
elm.checked = false;
}
}
current.checked = true;
}
</script>
CS
protected void repeaterImages_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
try
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
RadioButton rbLogoSeleccionado = (RadioButton)e.Item.FindControl("rbLogoSeleccionado");
RadioButton rbLogoSeleccionadoApp = (RadioButton)e.Item.FindControl("rbLogoSeleccionadoApp");
RadioButton rbLogoSeleccionadoAppBlanco = (RadioButton)e.Item.FindControl("rbLogoSeleccionadoAppBlanco");
string script = "SetUniqueRadioButton('rbLogoSeleccionado',this)";
string scriptApp = "SetUniqueRadioButton('rbLogoSeleccionadoApp',this)";
string scriptAppBlanco = "SetUniqueRadioButton('rbLogoSeleccionadoAppBlanco',this)";
rbLogoSeleccionado.Attributes.Add("onclick", script);
rbLogoSeleccionadoApp.Attributes.Add("onclick", scriptApp);
rbLogoSeleccionadoAppBlanco.Attributes.Add("onclick", scriptAppBlanco);
}
}
catch (Exception ex)
{
PIPEvo.Log.Log.RegistrarError(ex);
throw;
}
}
I tried:
Surrounding the radio buttons by LinkButton (and use Click and Command event... None is raising...). It Didnt work.
Define the ItemCommand as usual (trick CauseValidation="false" as well). It didnt work. (1)
Setting the link between linkbutton and his function event programatically (as radio button). It didnt work.
ITEMCOMMAND CODE (1)
CSHTML
<asp:Repeater runat="server" ID="repeaterImages" OnItemDataBound="repeaterImages_ItemDataBound" OnItemCommand="repeaterImages_ItemCommand">
<ItemTemplate>
<asp:LinkButton ID="lnkEliminarImagen" runat="server" OnClick="lnkEliminarImagen_Click" CausesValidation="false"><i class="fa fa-times" ></i></asp:LinkButton>
<span>
<asp:LinkButton runat="server" ID="lnkbtnbtn" OnCommand="lnkbtnbtn_Command" CommandArgument='<%#Eval("LOGO_ID").ToString()%>' CausesValidation="false" CommandName="prueba"></asp:LinkButton>
<asp:RadioButton runat="server" ID="rbLogoSeleccionado" Text='Logo 0' GroupName="nombreLogo" /><br />
<asp:RadioButton runat="server" ID="rbLogoSeleccionadoApp" Text='Logo 1' GroupName="nombreLogoApp" /><br />
<asp:RadioButton runat="server" ID="rbLogoSeleccionadoAppBlanco" Text='Logo 2' GroupName="nombreLogoAppBlanco" />
<asp:HiddenField runat="server" ID="hdLogoId" Value='<%#Eval("id").ToString()%>' />
</span>
</ItemTemplate>
</asp:Repeater>
CS
protected void repeaterImages_ItemCommand(object source, RepeaterCommandEventArgs e)
{
HiddenField id = (HiddenField)e.Item.FindControl("hdLogoId");
string idlogo = id.Value;
switch (e.CommandName)
{
case ("prueba"):
string idid = e.CommandArgument.ToString();
break;
}
}
IDEA I AM LOOKING FOR
Associate each radiobutton to its image (through id) respectively.
When I, finally, submit I could do my stuff, knowing radiobutton (action I will do) and image (logo_id) specific

How do I make a postback trigger concurrently?

I have a TextboxSectionName, TextboxSectioncode and Checkbox. When Checkbox gets checked, a text in the TextboxSectionname will be added into TextboxSectioncode, TextboxSectioncode will be read-only, and gets background color; When unchecked, TextboxsectionName gets back.
The problem is process above works fine, but if I try to type more text into textboxSectionName when TextboxSectionCode is still checked, TextboxSectionCode doesn't take a text that has been added to textboxSectionName. To get it working, I have to uncheck the checkbox and check it again.
Is there any way to make the TextboxSectionCode take newly-added text from TextboxSectionName?
Here I have this block of code.
aspx
<asp:CheckBox ID="CheckBoxSectionCode" runat="server" Style="margin-left: 5px;" OnCheckedChanged="CheckBoxSectionCode_CheckedChanged" AutoPostBack="True" />
<asp:TextBox ID="TextBoxSectionName" runat="server" Width="250px" onkeyup="RefreshUpdatePanel();" ></asp:TextBox>
<asp:TextBox ID="TextBoxSectionCode" runat="server" Width="250px" ></asp:TextBox>
code behind checkbox event handler
protected void CheckBoxSectionCode_CheckedChanged(object sender, EventArgs e)
{
if (CheckBoxSectionCode.Checked == true)
{
TextBoxSectionCode.Text = TextBoxSectionName.Text;
TextBoxSectionCode.BackColor = Color.LightGray;
TextBoxSectionCode.ReadOnly = true;
}
else
{
TextBoxSectionCode.ReadOnly = false;
TextBoxSectionCode.Text = "";
TextBoxSectionCode.BackColor = Color.White;
}
}
Thanks,
Phillip

How to force enable/disable of controls in aspx?

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).

RadComboBox pre selection

First of all what I want to do is to pre select a value in my RadComboBox ,and if this value is not selected something else is selected then change the visibility to of some specific fields hidden.
My problem is that I'm able to make my pre select but somehow I can not change the status of my visibility for my specific fields when this pre selected value has changed.
What I have tired is to do it with a standard event OnSelectedIndexChanged but some how this is not triggering why so ever.. I have also added AutoPostBack=true as well as ViewStateMode=Enabled"
First my field's
Here comes my preslect as well here I would like to trigger the visibility change
<div class="formRowDiv">
<asp:Label ID="Activitylbl" runat="server" Text="Activity" CssClass="formLabel" />
<telerik:RadComboBox ID="rcbActivity" CssClass="rowForm" ViewStateMode="Enabled" runat="server" Width="260px" EmptyMessage="- Activity -"
DataTextField="ActivityId" DataValueField="ActivityId" AutoPostBack="true" OnSelectedIndexChanged="rcbActivity_SelectedIndexChanged">
</telerik:RadComboBox>
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="rcbActivity"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
What I want to hide:
<div class="formRowDiv">
<asp:Label ID="ActivityDescription" runat="server" Text="ActivityDescription" CssClass="formLabel" Visible="false"/>
<telerik:RadTextBox runat="server" ID="rtbActivityDescription" Wrap="true" Height="50" TextMode="MultiLine" AutoPostBack="true" CssClass="rowForm" ReadOnly="true" Visible="false" />
</div>
How I do my pre selection :
In my databind method that is called in my Page_Load
I firrst loop and then do a pre select
foreach (Activity item in ctx.Activity.OrderBy(l =>l.Code))
{
rcbActivity.Items.Add(new RadComboBoxItem(item.FullActivity, item.ActivityId.ToString()));
if (rcbActivity.Items.FindItemByValue("4") != null)
{
rcbActivity.SelectedIndex = rcbActivity.Items.IndexOf(rcbActivity.Items.FindItemByValue("4"));
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
}
Here is how I would hide my Fields
protected void rcbActivity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
In case your controls are in an update panel then try removing it if the update panel is not so important and see if the changes u make to the controls in the server side are getting affected properly

Hide and Show Label and Button

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;
}

Categories

Resources