asp.net radiobuttonlist will not selectindex - c#

I have a radio button list...
<asp:RadioButtonList ID="rblCollectOptions" runat="server" CssClass="radiolist">
<asp:ListItem Value="Collect" Text="Collect from this address"></asp:ListItem>
<asp:ListItem Value="DropOff" Text="Drop off at Depot (UK only)"></asp:ListItem>
</asp:RadioButtonList>
I also have a link button on the page to "enter address manually" which I want to set the radio button to "Collect" value.
I tried...
rblCollectOptions.SelectedIndex = 0;
and
rblCollectOptions.Items[0].Selected = true;
both work if no option is already selected, but if I manually set the radio button to another option, or set a default selection, the link button does not work.

Call ClearSelection or set SelectedIndex = -1 before you set the selected item.
rblCollectOptions.ClearSelection();
or
rblCollectOptions.SelectedIndex = -1;

Related

Have Validation with DropDownList

I have a DropDownList that is binded to a DataSource on the PageLoad of a page. In this DropDown List I've added a Select value as the default value. On the bottom of this drop down list and some textboxes I have a Add button. How do I add a validation on the dropdown list so that if the User leaves the value as "Select" he/she can not Click the add button unless he/she selects another value.
<asp:Label ID="lblItems" runat="server" Text="SemesterCode: "></asp:Label>
<asp:DropDownList ID="ddlItems" AppendDataBoundItems="true" runat="server">
<asp:ListItem Text=" -- Select -- " Value="-1"></asp:ListItem>
</asp:DropDownList>
Use the "onchange" method for a drop down list. When the value of the DDL is changed call a JS function that will enable the other buttons/required fields.
function validateThingy(val) {
return (val != -1)
}
function calledOnChange(val) {
if (validateThingy(val)) {
//enable the buttons
} else {
//Keep the button disabled
}
}
Check Selected index of your dropdownlist If it is 0 so alerts a message else puts your logic likewise
if(ddlItems.SelectedIndex==0){
Response.Write("Please choose any value");
}else{
//Put your logic here
}

SelectedIndexChange not firing in asp.net

I'm using dropdownlist in asp.net which contains 10,20,50 as values.
I'm using gridview to display dataretrieve from table based on the value selected in dropdownlist.
Example, when I select 20, the gridview should display only 20 rows.
I'm using the following coding;
protected void ddlRowPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtCurrentPage = sender as TextBox;
DropDownList ddlRowPerPage = sender as DropDownList;
int startRowIndex = 0;
pager.PageSize = Convert.ToInt32(ddlRowPerPage.SelectedValue);
Response.Cookies[hdnRowPerPageName .Value].Value = pager.PageSize.ToString();
pager.SetPageProperties(startRowIndex, Convert.ToInt32(ddlRowPerPage.SelectedValue), true);
}
I've 30 rows in table.
My problem is, when I select 50, it shows all rows. But when I select 10, the SelectedIndex function is not firing.
At the same time, after selecting 50, when I select 20, the selectedindex is firing.
What is the problem?
Here is the updates .aspx page coding:
<div class="pull-left">
<asp:DropDownList ID="ddlRowPerPage" runat="server" OnSelectedIndexChanged="ddlRowPerPage_SelectedIndexChanged"
EnableViewState="true" AutoPostBack="true" Width="60px" >
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
rows per page
</div>
Problem : as your items order is 10,20,50...etc., it is cleared that 10 is at first location and when you select the 10 as first selection Index will not get changed.
Reason: IndexChanged Event only fires when SelectedItem Index is changed.
ut when you select some other item 20 or 50 and then select 10 it definitely fires.
Solution :
Add a Default item to DropDownList as --Select Item-- so that whenever user selects item 10 it fires the event as Selected Index is changed.
Try This:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" Height="28px" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" Width="201px">
<asp:ListItem>-Select Item-</asp:ListItem>
<asp:ListItem>10</asp:ListItem>
<asp:ListItem>20</asp:ListItem>
<asp:ListItem>50</asp:ListItem>
</asp:DropDownList>
The Problem is in not understanding when this event can be fired.. it can fired only when the index of the dropdownlist changed and that didn't happened in your case, because the first element when the dropdownlist rendered is 10.
In my humble opinion, the optimum solution according to that issue is rendering the dropdownlist in the page with a primer default selection like that :
<div class="pull-left">
<asp:DropDownList ID="ddlRowPerPage" runat="server" OnSelectedIndexChanged="ddlRowPerPage_SelectedIndexChanged"
EnableViewState="true" AutoPostBack="true" Width="60px" AppendDataBoundItems="true">
<asp:ListItem Value="0">--Choose--</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="20">20</asp:ListItem>
<asp:ListItem Value="50">50</asp:ListItem>
</asp:DropDownList>
rows per page
</div>
protected void ddlRowPerPage_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox txtCurrentPage = sender as TextBox;
DropDownList ddlRowPerPage = sender as DropDownList;
int startRowIndex = 0;
pager.PageSize = Convert.ToInt32(ddlRowPerPage.SelectedValue);
Response.Cookies[hdnRowPerPageName.Value].Value = pager.PageSize.ToString();
pager.SetPageProperties(startRowIndex, Convert.ToInt32(ddlRowPerPage.SelectedValue), true);
}
Add ViewStateMode="Enabled" to the DropDownList it should work.
I got information from here quite often.
I just want to share my experience this time and hope it might help someone someday.
I have a DropDownList in my GridView's PagerTemplate.
I used the example code from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.pagertemplate(VS.80).aspx
I used it in many of my pages. All works fine except one.
The OnSelectedIndexChanged event just isn't fired in that one page.
Later I set the "EnableViewState" of that GridView to false, it works finally.

Radio Button List not properly selecting new value

I am having trouble getting a radio button from a Radio Button List to become selected in an IF statement. Everything else is working correctly when the IF is true, except for this. Request Pending is the default value, but I need to be able to have the "Waiting for Approval" Button selected when the IF is True.
My HTML code is:
<asp:RadioButtonList ID="rbGiftStatus" RepeatDirection="Horizontal"
runat="server" TabIndex="3">
<asp:ListItem Text="Request Pending" Selected="True" Value="1"></asp:ListItem>
<asp:ListItem Text="Check Pending" Value="2"></asp:ListItem>
<asp:ListItem Text="Completed" Value="3"></asp:ListItem>
<asp:ListItem Text="Waiting for Approval" Value="4"></asp:ListItem>
</asp:RadioButtonList>
My C# is this:
rbGiftStatus.SelectedIndex = 4;
and I have tried other ways suchs as:
rbGiftStatus.Text = "4";
rbGiftStatus.SelectedItem = "4";
rbGiftStatus.SelectedValue = "4";
None of them seem to work, and I can't figure out why
SelectedIndex is the correct way: Set Radiobuttonlist Selected from Codebehind
But, you are using SelectedIndex of 4, which is out of range of the array. C# is 0-based indexing, so the first item is index 0. That would make your 4th item index 3.
rbGiftStatus.SelectedIndex = 3; should do it.
You can try with
rbGiftStatus.SelectedIndex = 3; //index max is 3 for 4 elements
The index starts with a 0 and not a 1. So if you have 4 items in your RBL then the index range will be 0-3. In this case you are calling an index of 4 which does not exists.
Try rbGiftStatus.SelectedIndex = 3;

Radio button selection should not be changed by any one

I have made a radio button list in an aspx page in C#
<asp:RadioButtonList ID="RblMentor_teacher_student" class="case" runat="server"
RepeatDirection="Horizontal" AppendDataBoundItems="False" >
<asp:ListItem Text="Docent" Value="0"></asp:ListItem>
<asp:ListItem Text="Mentor" Value="1"></asp:ListItem>
<asp:ListItem Text="Student" Value="2"></asp:ListItem>
</asp:RadioButtonList>
It works perfectly as I want that it shows the selected person is either teacher or student or mentor.
But I want that no one can change the selection of this.
Can anyone help.
You could put this in your Page_load() and it would disable the whole list.
RblMentor_teacher_student.Enabled = false;
Or you could disable a specific list item
<asp:ListItem Text="Docent" Value="0" Disabled="Disabled"></asp:ListItem>
You can set Enabled property to false
On selectedindexchanged event of radiobutton..then put enable property false for selected item
Using jQuery:
Benefit of using this is that the checked value will be always post on submit.
$(function () {
$("input[type=radio]", $("#<%= RblMentor_teacher_student.ClientID%>")).each(function (index) {
if ($(this).attr("checked") != "checked") {
$(this).attr("disabled", "disabled");
}
});
});

respond to asp:RadioButtonList changes with jquery

I have the code below with sets the focus on a textbox when the page loads and when the user selects a radio button.
How can I change this so that if they choose the last radio option "Mail" the focus is not set to
txtPayerName but instead to the btnSubmit control?
$(function () {
SetDefaultPaymentType();
$("#txtPayerName").focus();
$("#rdLstPaymentOptions").change(function () {
$("#txtPayerName").focus();
});
});
<asp:RadioButtonList ID="rdLstPaymentOptions" runat="server" RepeatColumns="3" RepeatDirection="vertical"
RepeatLayout="Flow" OnPreRender="rdBtnLst_PreRender" TabIndex="1"
ClientIDMode="Static">
<asp:ListItem Text="Credit Card " Value="CreditCard" ></asp:ListItem>
<asp:ListItem Text="Electronics Fund Transfer " Value="EFT"></asp:ListItem>
<asp:ListItem Text="Check By Mail " Value="Mail"></asp:ListItem>
</asp:RadioButtonList>
What if you set the change event on each radio button individually?
Something like:
$("#rdLstPaymentOptions").each(function (index) {
if (index == $("#rdLstPaymentOptions").length - 1) {
// insert code to set change event for last radio button
}
else {
($this).change = function () {
$("#txtPayerName").focus();
}
}
});
Give the radio button and the submit button a unique CSS class, then use:
$('.radioClass').click(function() {
$('.submitClass').focus()
})

Categories