How to retrieve value from DropDownList in List View? - c#

I'm trying to retrieve the value in my dropdownlist that is in my list View.
This is my dropdownlist:
<asp:DropDownList ID="ddlSize" runat="server" CssClass="ddl" AppendDataBoundItems="true" >
<asp:ListItem Value="S">Small +RM5.00</asp:ListItem>
<asp:ListItem Value="M">Medium +RM10.00</asp:ListItem>
<asp:ListItem Value="L">Large +RM15.00</asp:ListItem>
<asp:ListItem Value="X">Xtra-Large +RM20.00</asp:ListItem>
</asp:DropDownList>
This is the current way which I used to retrieve the value of my dropdownlist.
protected void prodList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
size = (e.Item.FindControl("ddlSize") as DropDownList).SelectedValue;
}
By using the method above and got an error Object reference not set to an instance of an object which means im passing a null value.

According to the article, OnItemCommand is only triggered if you have a button inside your ListView Control clicked, are you sure that you had the button already, if yes, I think it should work because I've replicated your issue on my machine?
Article

Related

First record in dropdown list will not update the other dropdownbox

I have a dropdown box that is populated from results from a SQL query. The selected value from dropdownlist1 successfully populates the lbAuthors dropdown list. During testing I realized that the first record from dropdownlist1 never updates into the lbAuthors dropdownlist. Here is an example: if I have three authors name in the 2nd dropdown box (Frost, Kipling, Poe) the first name - Frost - does not update into the first dropdown box. Kipling or Poe do - but not Frost.
My question is - What do I need to include in my event to allow Frost (or whatever the first record is) to update into the first dropdown box? –
Code-behind:
protected void update_SelectedItem(object sender, EventArgs e)
{
lbAuthorList.Items.Clear();
lbAuthorList.Items.Add(new ListItem(DropDownList1.SelectedItem.Text, DropDownList1.SelectedItem.Text));
lbAuthorList.Items.FindByValue(DropDownList1.SelectedItem.Text).Selected = true;
}
Markup:
<asp:DropDownList runat="server"
ID="lbAuthors"
style="float:left;"
DataSourceID="odsAuthorList"
DataTextField="DisplayAuthorName" DataValueField="AuthorID"
onselectedindexchanged="lbUserList_SelectedIndexChanged"
AppendDataBoundItems="True" >
</asp:DropDownList>
<asp:DropDownList ID="DropDownList1"
runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
</asp:DropDownList>
That is because when you select the first item, selcted index does not change. You need to insert a dummy item like this::
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="True"
DataSourceID="SqlDataSource2"
DataTextField="Display_AuthorName"
EnableViewState="false"
DataValueField="Display_AuthorName"
OnSelectedIndexChanged="update_SelectedItem"
AutoPostBack="true">
<asp:ListItem Text="--Select One--" Value="-1"></asp:ListItem>
</asp:DropDownList>
This issue rises because u wrote code for dropdownlist changed but initially first value selected at that time dropdownlist changed event not fired. If u choose second and then choose first one it will work fine.
Just add one dummy variable in dropdownlist item..
dropdownlist1.Items.Add("--Select--");

Selected Index doesn't change

I have an ASP.NET DropDownList control, with a onSelectedIndexChanged event. I also have the AutoPostBack="true" that many have said would fix the problem. However I don't think that is where the problem lays... My Html code and C# code are below for reference. The thing is the code works, but only when I press the enter key while editing the drop down box. If I simply click on an object in the drop down then the event will not fire. If I change the selected item so the "selected" text in the drop down says "ASP" and I then inspect the element using the browser I see that the Selected="True" part of the ListItem is still on the first item... It doesn't change in there. It changes with an enter key but not with a mouse click. Any help is welcome and much appreciated.
HTML:
<div class="ui-widget">
<asp:DropDownList id="Select1" OnSelectedIndexChanged="Select1_SomethingChange" runat="server" AutoPostBack="true">
<asp:ListItem Selected="True" Value="White"> White </asp:ListItem>
<asp:ListItem Value="Select one...">Select one...</asp:ListItem>
<asp:ListItem Value="ActionScript">ActionScript</asp:ListItem>
<asp:ListItem Value="AppleScript">AppleScript</asp:ListItem>
<asp:ListItem Value="Asp">Asp</asp:ListItem>
<asp:ListItem Value="BASIC">BASIC</asp:ListItem>
</asp:DropDownList>
</div>
C#:
protected void Select1_SomethingChange(object sender, EventArgs e)
{
//something is meant to happen here
}
It may be caused by data binding your dropdownlist in Page_Load method.
Please, surround it (data binding) with
if(!IsPostBack){
// data binding.
}
Hope, it help!
AutoPostBack="true"
maybe you miss this option...
Your code works fine, there could be something in code which changes the implementation. I have debug your code and it's showing the selected item in output window. Please verify if there is some javascript code which is causing issue to call dropdown selectedIndexChanged event.
protected void Select1_SomethingChange(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
Debug.WriteLine(ddl.SelectedItem.Text);
}

checkbox list to generate one textbox

So i have this checkbox list and im trying to generate a textbox when the MISC checkbox is checked.
<asp:CheckBoxList ID="chbxEquipmnt" runat="server"RepeatColumns="4" RepeatDirection="Horizontal" ValidationGroup="Equipment" OnSelectedIndexChanged="chbxEquipmnt_SelectedIndexChanged">
<asp:ListItem Value="Laptop">Laptop</asp:ListItem>
<asp:ListItem Value=" Label Printer">Label Printer</asp:ListItem>
<asp:ListItem Value="Printer">Printer</asp:ListItem>
<asp:ListItem Value="Fax Line">Fax Line</asp:ListItem>
<asp:ListItem Value="PC">PC</asp:ListItem>
<asp:ListItem Value="MFD">MFD</asp:ListItem>
<asp:ListItem Value="Phone Line">Phone Line</asp:ListItem>
<asp:ListItem Value="Misc">Misc</asp:ListItem>
</asp:CheckBoxList>
ON my Cs page I have.........
protected void chbxEquipmnt_SelectedIndexChanged(object sender, EventArgs e)
{
if (chbxEquipmnt.SelectedValue == "Misc")
{
TextBox txt = new TextBox();
txt.ID = "txtMiscCheckBox";
Page.Form.Controls.Add(txt);
}
}
debugging i have tried with Postback which continuously on returns the first check box that was checked...for example i checked laptop instead of Misc first, the value in the debugger always show me laptop. Im not sure if a simple loop to go thru the all the button clicks would help.....I'm fresh out of school and this is a career change for me so thanks for the patience
I would change the approach: don't create the Textbox at run time.
Have it created at design time, hide it by default (Visible property), then show it when Misc option is selected.
You will run into less issues that way. Creating controls in runtime require some extra effort : dealing with ViewState, re-creating the control on PostBack, etc.

ASP.Net DropDownList OnSelectedIndexChange not firing

I have the following DropDownList control:
<asp:label runat="server" text="Filter by state: "></asp:label>
<asp:dropdownlist runat="server" id="filterbystate"
OnSelectedIndexChanged="FilterByState">
<asp:ListItem value="all" selected="True">All</asp:ListItem>
<asp:ListItem value="ca" selected="False">California</asp:ListItem>
<asp:ListItem value="co" selected="False">Colorado</asp:ListItem>
<asp:ListItem value="id" selected="False">Idaho</asp:ListItem>
<asp:ListItem value="ut" selected="False">Utah</asp:ListItem>
</asp:dropdownlist>
Here is the method:
protected void FilterByState(object sender, EventArgs e)
{
var value = e;
}
The method will not fire for some reason. I select a different value and nothing happens. What I am trying to do is reload the page passing in the state value so I can filter the results by it.
What am I doing wrong?
Set AutoPostBack=True as an attribute of your DDL and it will automatically post back the selected index change event
Add this to dropdown list aspx it will cause a request to be send to the server and your event will be fired.
AutoPostBack="true"
You just need to set AutoPostBack = True
From ListControl.AutoPostBack property;
Gets or sets a value indicating whether a postback to the server
automatically occurs when the user changes the list selection.
AutoPostBack="true" and
write page load event
if (!IsPostBack)
{
DDL_Designation_Bind();
}
// Because autopostback properties fire load event then our dropdownlist rebind and always selected index 0 so Not Rebinding dropDownlist

Getting a dropdownlist to check a checkbox in asp.net/C#

<input runat ="server" type ="checkbox" id="helprequest" />
<label for="helprequest">Help request</label>
<asp:DropDownList ID="options" runat="server" OnSelectedIndexChanged="checkHelpRequest">
<asp:ListItem Text="Windows"></asp:ListItem>
<asp:ListItem Text="Macintosh"></asp:ListItem>
<asp:ListItem Text="Linux"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
In my codebehind, I have
protected void checkHelpRequest(object sender, EventArgs e)
{
helprequest.Checked = true;
}
But when I select something on the dropdownlist, the checkbox, does not get marked as checked, how do I get the checkbox to appear as checked when I change the index on a dropdownlist?
Your DropDownList does not have AutoPostBack='true' set. Without setting this, your dropdown will not post back when you change the selected index.
Just change it to:
<asp:DropDownList AutoPostBack="true" ID="options"
runat="server" OnSelectedIndexChanged="checkHelpRequest">
Without setting this, your checkHelpRequest method will still be called when your drop down changes index, but only after a postback is caused by some other control, like a button, or another DropDownList that does have AutoPostBack set.

Categories