I have a problem, when I try to get selectedindex from asp:listbox in codebehind, it always stays -1, even thought on the page it is selected. I am fully updating this list every minute. For loading listItems, the entire list gets erased and written back again.
Code samples:
<asp:ListBox ID="ListBox1" runat="server"
OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"
AutoPostBack="false">
</asp:ListBox>
In codebehind:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
itemsIndex = ListBox1.SelectedIndex.ToString(); //It is always -1
itemToBeRescheduled = ListBox1.SelectedItem.Value;
}
Tried using if(!isPostBack).. but the index stayed -1 and it only erased my items from the list.
Thanks in advance!
In my experience, it happens when you rebind ListView on postback again. In order to prevent it, we normally place binding inside !IsPostback.
For example,
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox1.DataSource = new List<ListItem>()
{
new ListItem("One", "1"),
new ListItem("Two", "2"),
new ListItem("Three", "3")
};
ListBox1.DataBind();
}
}
When I have:
<asp:ListBox ID="ListBox1" runat="server" Height="140px" Width="290px" Font-Size="22px" autopostback="true" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
<asp:ListItem>Item 1</asp:ListItem>
<asp:ListItem>Item 2</asp:ListItem>
<asp:ListItem>Item 3</asp:ListItem>
<asp:ListItem>Item 4</asp:ListItem>
<asp:ListItem>Item 5</asp:ListItem>
<asp:ListItem>Item 6</asp:ListItem>
</asp:ListBox>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
And:
protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (ListBox1.SelectedIndex == -1)
TextBox1.Text = "-1";
else
TextBox1.Text = ListBox1.SelectedItem.Text;
}
I don't get ListBox1.SelectedIndex = -1.
Related
I have a Radiobuttonlist and I want after the Item with value 1 is selected to do something. When I used RadioButtonListType.SelectedValue is returning "" and when I use RadioButtonListType.SelectedIndex is returning -1.
Where I am doing wrong ?
this is my code
aspx
<asp:RadioButtonList ID="RadioButtonListType" runat="server" OnSelectedIndexChanged="RadioButtonListType_SelectedIndexChanged" ValidationGroup="emergency" AutoPostBack="True">
<asp:ListItem Value="0">Normal</asp:ListItem>
<asp:ListItem Value="1">Emergency</asp:ListItem>
</asp:RadioButtonList>
.cs
protected void RadioButtonListType_SelectedIndexChanged(object sender, EventArgs e)
{
if (RadioButtonListSF.SelectedIndex == 1)
{
MPEEmergency.Show();
}
}
I designed a simple page with, two text boxes, one checkbox, one button, and one label.
When I start I want to check the checkbox to make the button enabled, and then enter two numbers into the two textboxes, click the button to do addition, and show the result in the label.
But when I click the checkbox the page postback is not working; it's not writing Page is posted back on the page and the button is still disabled.
However, if I make the button enabled and do the addition it invokes the page postback and also invokes the checkedchanged method.
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<td>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"
onCheckedChanged="cboptions_CheckedChanged" />
</td>
<asp:Button ID="submit" runat="server" Text ="addition" onclick="Button_Click"/>
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Response.Write("Page is posted back");
}
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
submit.Enabled = cboptions.Checked;
}
protected void submit_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirst.Text);
int b = Convert.ToInt32(txtSecond.Text)+a;
result.Text = b.ToString();
}
There were many formatting errors in your code, do it this way
Aspx
<asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>
<asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>
<asp:Label ID="result" runat="server"></asp:Label>
<asp:CheckBox ID="cboptions" runat="server" AutoPostBack="True"
onCheckedChanged="cboptions_CheckedChanged" />
<asp:Button ID="btn" runat="server" Text ="addition" onclick="Button_Click"/>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == true)
{
Response.Write("Page is posted back");
}
}
protected void cboptions_CheckedChanged(object sender, EventArgs e)
{
btn.Enabled = cboptions.Checked;
}
protected void Button_Click(object sender, EventArgs e)
{
int a = Convert.ToInt32(txtFirst.Text);
int b = Convert.ToInt32(txtSecond.Text) + a;
result.Text = b.ToString();
}
I have a drop down list and the values I get them from SQL.
There are 4 choices. I need on one of the choices to turn a textbox.visible = false;
I am not sure that is correct. I have it in SQL as Cancel_Reason
protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
string Item = ddlCancelReason.SelectedValue;
if (Item == "Non-Payment")
{
tbReturn.Visible = false;
}
}
Did you bind a SelectedIndexChanged event to your DropDownList? If you did:
In your case it won't work because you didn't enable the AutoPostBack-property of the DropDownList.
Change your DropDownList code from:
<asp:dropdownlist id="ddlCancelReason" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
To:
<asp:dropdownlist id="ddlCancelReason" AutoPostback="true" runat="server" datatextfield="Cancel_Reason" datavaluefield="ID"> </asp:dropdownlist>
Just add AutoPostback="true".
Then this will work:
protected void ddlCancelReason_SelectedIndexChanged(object sender, EventArgs e)
{
string Item = ddlCancelReason.SelectedValue;
if (Item == "Non-Payment")
{
tbReturn.Visible = false;
}
}
I have four dropdownlistbox in my form, i want to load items into three dropdownlists according to the selection of items from the first drop down list , my first dropdown list has folloding items
Amount ,
PAC ,
Base UOM
Whatever i am selecting from first drop down list , i want to load the same selected item into remaining three dropdownlist
I have tried the following code but it is not working as expected
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
pls help.
You need to use Ajax Control Tool kit's Cascaded Drop-down
Here is the like for it:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/Walkthrough/CCDWithDB.aspx
and here is the demo:
http://www.asp.net/AjaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx
here is a tutorial link:
http://www.dotnetfox.com/articles/ajax-cascading-dropdown-example-with-database-in-Asp-Net-1078.aspx
Hope this material helps.
May be you don't have set AutoPostBack property to true. Try below code sample :
ASPX:
<asp:DropDownList runat="server" ID="parentDropDown" OnSelectedIndexChanged="parentDropDown_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="1" Value="1"></asp:ListItem>
<asp:ListItem Text="2" Value="2"></asp:ListItem>
<asp:ListItem Text="3" Value="3"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child1">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="child2">
</asp:DropDownList>
Code Behind:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
If you want to remove existing item from child before you add then:
protected void parentDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = parentDropDown.SelectedValue;
child1.Items.Clear();
child2.Items.Clear();
child1.Items.Add(uom_Name);
child2.Items.Add(uom_Name);
}
You have to set AutoPostBack="true" to the first drop down list and clear other drop downs items before adding new item.
ASPX:
<asp:DropDownList ID="ddl_UOM" runat="server" OnSelectedIndexChanged="ddl_UOM_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Amount" Value="Amount"></asp:ListItem>
<asp:ListItem Text="PAC" Value="PAC"></asp:ListItem>
<asp:ListItem Text="Base" Value="Base"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl_UOM2" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM3" runat="server"></asp:DropDownList>
<asp:DropDownList ID="ddl_UOM4" runat="server"></asp:DropDownList>
ASPX.CS
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM2.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM3.Items.Clear();
ddl_UOM3.Items.Add(new ListItem(uom_Name, uom_Name));
ddl_UOM4.Items.Clear();
ddl_UOM4.Items.Add(new ListItem(uom_Name, uom_Name));
}
protected void ddl_UOM_SelectedIndexChanged(object sender, EventArgs e)
{
string uom_Name = ddl_UOM.SelectedItem.Value;
ddl_UOM2.Items.Clear();
ddl_UOM3.Items.Clear();
ddl_UOM4.Items.Clear();
ddl_UOM2.Items.Add(uom_Name);
ddl_UOM3.Items.Add(uom_Name);
ddl_UOM4.Items.Add(uom_Name);
}
After writing code in ddl_UOM_SelectedIndexChanged() , call ddl_UOM_SelectedIndexChanged() method from Page_Load()
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
ddl_UOM_SelectedIndexChanged(sender, e );
}
}
catch (Exception)
{
throw;
}
}
You should try to use PageMethods in JS to load the dropdowns any way you like any time you like.
Problem:
SelectedIndexchanged does not fire. I tried investigating with a breakpoint but it does not even get to the event.
I made the event by double clicking on the combobox. But it did not help.
Please advice.
Here is the code:
protected void nav_dd_parent_edit_SelectedIndexChanged(object sender, EventArgs e)
{
}
<td width="55%" class="style1" height="20px">
<asp:DropDownList ID="nav_dd_parent_edit" runat="server"
DataSourceID="sp_GetNavParents_Edit" DataTextField="Name"
DataValueField="NavItemId" Height="24px" ReadOnly="FALSE" Width="375px"
onselectedindexchanged="nav_dd_parent_edit_SelectedIndexChanged">
</asp:DropDownList>
</td>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Page.MaintainScrollPositionOnPostBack = true;
//SiteMaster.g_solution = "Couche-Tard - QV";
//SiteMaster.g_solution_id = 27;
nav_dd_parent.DataBind();
if (SiteMaster.g_solution != null && SiteMaster.g_solution != "")
{
nav_literal.Text = "Solution: " + SiteMaster.g_solution;
nav_hidden_SoltnId.Value = SiteMaster.g_solution_id.ToString();
}
else
{
nav_literal.Text = "Please select a solution first from the 'Solution Template' Tab.";
panel_top.Visible = false;
}
}
You're not seeing your breakpoint get hit because the dropdownlist is not posting back when the selection changes.
Set AutoPostBack to true and you should be all set.
I noticed your DropDownList Id is "nav_dd_parent_edit", but your Page_Load is calling the Databind method on "nav_dd_parent" - could that be part of the problem?
Anyway, I did a simplified version of your DropDownList that works fine - perhaps it might help.
<table>
<tr>
<td width="55%" class="style1" height="20px">
<asp:DropDownList
ID="nav_dd_parent"
runat="server"
DataTextField="Name"
DataValueField="NavItemId"
Height="24px"
ReadOnly="FALSE"
Width="375px"
onselectedindexchanged="nav_dd_parent_edit_SelectedIndexChanged"
AutoPostBack="true">
</asp:DropDownList>
</td>
</tr>
</table>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nav_dd_parent.Items.Add(new ListItem("Item 1", "1"));
nav_dd_parent.Items.Add(new ListItem("Item 2", "2"));
nav_dd_parent.Items.Add(new ListItem("Item 3", "3"));
}
}
protected void nav_dd_parent_edit_SelectedIndexChanged(object sender, EventArgs e)
{
int codeGetsHere = 0;
}
Few notes to keep in mind as below:
a. Set 'AutoPostBack' to true :
<asp:DropDownList ID="nav_dd_parent_edit" runat="server" AutoPostBack="true"
DataSourceID="sp_GetNavParents_Edit" DataTextField="Name"
DataValueField="NavItemId"
onselectedindexchanged="nav_dd_parent_edit_SelectedIndexChanged">
</asp:DropDownList>
b. Always bind on not postback time :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
nav_dd_parent_edit.DataSource = yourDataSourceName;
nav_dd_parent_edit.DataBind();
}
}