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;
Related
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.
hi i have gridview that when a row is selected, it populates into textboxes that are used to populate the gridview itself. the last field is a dropdownlist and its not displaying when the gridview is clicked. i set a breakpoint and see that its stuck on the first - 0 index. i dont know why it isnt moving forward... here is the code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
....
if (DropDownListCurrency.Items.FindByValue(row.Cells[7].Text.ToString().Trim) != null)
{
DropDownListCurrency.SelectedValue = row.Cells[7].Text.ToString().Trim();
}
....
}
<asp:DropDownList ID="DropDownListCurrency" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
why you want take the value from textbox. Will better use DataKeyNames like this inside that event
GridViewRow row = GridView.SelectedRow;
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Value);
this work if you have only one value in DataKeyName if you look not there a index if you want have more than one value use this
int id = Convert.ToInt32(GridView.DataKeys[row.RowIndex].Values["FirstValue"]);
string name = Convert.ToString(GridView.DataKeys[row.RowIndex].Values["SecondValue"]);
Does the DropDownList contains the words that you wanna show displayed?
You need to set the AutoPostBack property to True:
it should be :
<asp:DropDownList ID="DropDownListCurrency" AutoPostBack="True" runat="server"
CausesValidation="True"
DataSourceID="CurrencyDropDownListDataSource"
DataTextField="Currency" DataValueField="Currency_ID"
AppendDataBoundItems="True">
<asp:ListItem Value="0" Text="<Select>" Enabled="True" Selected="False"></asp:ListItem>
</asp:DropDownList>
made a simple error...
called the wrong field was supposed to be value (Currency_ID) and not the text (Currency)
DropDownListCurrency.SelectedValue = GridView1.DataKeys[row.RowIndex].Values["Currency_ID"].ToString().Trim();
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;
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");
}
});
});
I have a static dropdown with the following html.
<asp:DropDownList width="100px" ID="dropDownActive" Runat="server">
<asp:ListItem Text="Inactive" Value="0"/>
<asp:ListItem Text="Active" Value="1" />
</asp:DropDownList>
I try to populate the selected value based off of data, but it has the the value of 0 before I get to it and will not accept a new value.
dropDownActive.SelectedValue = (support.Active)? "Active": "Inactive";
The values of your dropdown are "0" and "1", not "Active" and "Inactive"
dropDownActive.SelectedValue = (support.Active)? "1": "0";
Your dropdown list uses 0 & 1 to represent inactive and active values. So when setting the value, try using those, not their text counterparts.
dropDownActive.SelectedValue = (support.Active)? "1": "0";
Try this:
dropDownActive.SelectedValue = (support.Active)? 1: 0;
The SelectedValue property of the ASP DropDownList control is based off of the Value attribute of the markup.
You need to change your code to...
dropDownActive.SelectedValue = (support.Active) ? 0 : 1;