.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

Dynamically add item to DropDownList??

Posted By:      Posted Date: September 08, 2010    Points: 0   Category :ASP.Net
I have an Edit page in my application that contains a DropDownList. This DDL is bound to an EntityDataSource that only shows the active "ProjectManagers". In the case where the user is editing a record that is assigned to an "inactive" ProjectManger, an "ArgumentOutOfRange" Exception is (rightfully) thrown by the DDL (because that Item wan't bound to the list, since it only binds "Active" ProjectManagers). So, I need to add this item to the DropDownList dynamically or at least add an "Inactive" item and then "select" that item in the code behind. (Although, adding an "Inactive" item would force the user to actually change the ProjectManager and that wouldn't be a good idea.) I have tried handling the OnDataBinding method with the following code:   protected void PreventErrorOnBinding(object sender, EventArgs e)    {        DropDownList theDropDownList = (DropDownList)sender;        theDropDownList.DataBinding -= new EventHandler(PreventErrorOnbinding);        theDropDownList.AppendDataBoundItems = true;        ListItem li = new ListItem(""<<Inactive>>", "0");     

View Complete Post

More Related Resource Links

Dynamically Populate a DVWP depending on selected item



I am wanting to create a dashboard page with several data view web parts which dynamically populate depending on what item is selected in a separate web part (drop down form or just a list of links).  Can someone please point me to a tutorial or documentation on how to pass data between web parts or to a DVWP?  I have searched for a few hours and not been able to find anything useful.



DropDownList problems : The ViewData item that has the key 'userTYPE_id_user_type' is of type 'Sy

Hello,I've been searching for that problem for hours...I have two table :          -user          -userTypeI'm trying to create a user, wich as a column id_userType, so I'm trying to do a dropdownlist with the userType List I've that error code : The ViewData item that has the key 'userTYPE_id_user_type' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'. // // GET: /User/Create public ActionResult Create() { //Send to the view, the userTypeList IEnumerable<SelectListItem> userTypeList = new SelectList(_userRepository.FindAllUserType().Distinct().ToList(), "id_user_type", "Description"); ViewData["userType_id_user_type"] = userTypeList; return View(); } here the post controller // // POST: /User/Create [HttpPost] public ActionResult Create(users user) { if (ModelState.IsValid) { try { _userRepository.addUser(user); _userRepository.save(); return RedirectToAction("Index"); } catch { return Vie

Dynamically (on client side) populating data control (repeater) when dropdown item is selected

Hello!I'm currently working on a project and I want to make as much things as possible performing on client side - with as little loading as possible.The project consist of three "main" pages. On one page I use jQuery DataTable plugin which is working great. One page has tabs (using jQuery). I want to put datacontrol (repeater) on this site and when  user selects one item in dropdown, data in repeater loads on client side, without performing the action on server.I wonder if this is possible and if somebody can give me some case to help with?Thanks!I hope I was clear enough. all the best

How to get item selected in dropdownlist

I'm searching and trying now for the last couple of hours, so I thought the community might help.I'm filtering the rows in a gridview using a ddlDate. Now, I want to request this selected item.But: When I load the page without clicking in the DropDownList, the ddlDate.SelectedValue is empty, and the ddlDate.SelectedItem.Text.ToString() gives a NullReferenceException.Is there any way to fix this without using a listbox?

Databound DropDownList Item Value Property Localization

I am using a databound DropDownList in which the item's Text field is set to a text representation of an identifier value of type int.  I am filling the Value field of the ListItem with the int value.  All of this is being done through the setup of the DataTextField and DataValueField settings of the DropDownList control. However, since both the item Value and Text fields are strings, obviously a conversion must be performed at the time of binding to convert the non-string value to a string value.  I am making the assumption that the ToString() method for the value is invoked in order to convert the int value into a string so that it can be stored in the Value property of the ListItem. Once selections are made, I read the Value property of the SelectedItem to perform an action.  It occurred to me that converting back to the original data type might present a problem if the wrong culture information is used to perform the conversion. My question is, in the case of non-string values being automatically converted for a databound DropDownList, what culture is used to perform that conversion?  My initial guess would be the CurrentCulture, simply because no object is required to supply a ToString() overload which allows the specification of an IFormatProvider object.  However, if the System.Convert object is used to perform the conversion

dropdownlist selected item problum

In gridview we have edit button whenever user click edit button i want to show particular data of dropdownlist as a selected data that is selected by user as a selected text of dropdownlistis there any solution ???

Dropdownlist not firing SelectedIndexChanged on first item with autopostback true

Hi: Has anyone ever found a work around for the well documented and well known bug, the dropdownlist bug? Specifically where the SelectedIndexChange event does not fire on the first listitem, althought both autopostback and enableviewstate are true? In other words, if the first three items in the dropdownlist are red, blue, green, if you select green, the SelectedIndexChanged will fire, but if you then select red, the first item in the ddl, the event will not fire. All listitems will fire the SelectedIndexChanged event, except for the very first listitem. The HACK has always been to include a dummy item as the first list time like"Select a Color" to get around the first item bug. I have got a situation where I cannot use that hack. Ideas?  TIA, Bill  

Assign Value to DropDownList in GridView Dynamically

Hi, I got GridView with 2 columns both are Template field with drop down list. What I need to do is someting like when user select course 3/3A/2A/2B , the type column will auto select pratical option dynamically. When select RTT /FTT/BTT , the type column will select Theory option. Can someone tell me how to work on this thing. Thank you. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">  <Columns>  <asp:TemplateField HeaderText="Course">  <ItemTemplate>  <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True">  <asp:ListItem>2A</asp:ListItem>  <asp:ListItem>2B</asp:ListItem>  <asp:ListItem Value="3">3</asp:ListItem>  <asp:ListItem>3A</asp:ListItem>  <asp:ListItem>BTT</asp:ListItem>  <asp:ListItem>RTT</asp:ListItem>  <asp:ListItem>FTT</asp:ListItem>  <asp:ListItem></asp:ListItem>  </asp:DropDownList>  </ItemTemplate>  </asp:TemplateField>  <asp:TemplateField HeaderText="Type">  <ItemTemplate>  <asp:DropDownList ID="DropDownList2" runat="server">  <asp

disable a single intem in dropdownlist or stop the ddl change event for a single item


hello guys,

I am trying something apart of my boundries, is there any way to disable a single intem in dropdownlist or stop the ddl change event for a single item.

any feadback would be appriciable.

Thanks and Regards

Inderjeet Singh Khalsa

Is it possible to dynamically create Name (file name) when item saved to Doc Library?

Even though users have been instructed on a naming convention for saving documents to a library after creating a new one  from the library template, they forget or don't understand.  I would like to have the Name column (which is taken from the file name that it is saved as)  in the Document Library be a concatenation of other document meta data elements.  Is this possible?  In other words, is it possible for me to dynamically save the document with the correct file name as a Workflow action (in a Designer created workflow)?  I tried it, but without success.

unable to get dynamic generated dropdownlist item


hi sir,

i'm using asp.net 3.5 and what im trying to do is create the dropdownlist item dynamically base on value in each datarow, then user select from dropdownlist and click button to postback, when postback system should be able to know what value has been selected by user. the problem with following code is during postback, event of "OnItem_Created" get fired and e.Item.DataItem return null, therefore the dropdownlist will not get populated with the items and that caused the dropdownlist unable to return value selected by user. am i missing anything? is "OnItemCreated" the best place to dynamic create the dropdownlist item? any help will be appreciate. thanks!!

    <form id="form1" runat="server">
      <asp:Button runat="server" ID="btn" Text ="go" />
      <asp:DataGrid runat="server" ID="grd"  OnItemCreated="OnItem_Created">
              <asp:DropDownList runat="server" ID="ddl">

Select Item from Dropdownlist


I have a dropdown list created via MVCContrib FluentHTML - how do I perform a postback when the user selects any item in the dropdownlist? 

dynamically populating the database in the dropDownlist using c# in asp.net


hi everyone , plz listen my question very carefully

i have 2 tables  Desgn & EmpForm

Desgn table is lik this

CREATE TABLE [dbo].[Desgn](
    [DesignationCode] [int] IDENTITY(1,1) NOT NULL,
    [DesignationName] [varchar](50) NOT NULL,
    [DesignationCode] ASC

EmpForm table is like this

CREATE TABLE [dbo].[Empform](
    [EmpId] [varchar](50) NOT NULL,
    [EmpName] [varchar](50) NOT NULL,
    [EmpSalary] [numeric](18, 0) NULL,
    [EmpDesignationcode] [numeric](18, 0) NOT NULL,
    [EmpDateofBirth] [date] NOT NULL

now my ques is i want to dynamically populate the dropdown list of designation on the basis of the code but instead of code name should get populated .i have written the code like this 

public void Populate1()
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["co

Dynamically Generate No of Control(like Label) depend upon database no of item


I have a requirment like 

i required a dynamically generated Control (like Label) depend upon Database no of item


  If i fetch Database text (like Name) fieldd 3 time than i am intrested to generate  label 3 time

for (int i=0;i<table.row.count;i++)


label label[i]=table.row[i][0].Tostring();


How can do the above because label[i] is not allowed

Khurram Shahzad

Howto add selected item from dropdownlist to a textbox on the same form?


 I have been trying to figure out what the proper method is to be able to have a dropdownlist on the same form that holds a number of textboxes for properties. I am using a strongly typed typed view which was generated by the Visual Studio IDE so it contains textboxes for all of the properties of my strongly typed class.  The class has a few properties that can have a set of possilbe answers; I'd like to displat those possible answers in a dropdownlist and then when the user fills in the form and clicks the submit button the selected item would be transferred to the correct property. I have a solution but think there must be a better solution out there and was wondering if anyone might point me in the right direction.

So say I have a model with 3 properties, FirstName, LastName. and MaritalStatus.

If I generated a view for this then I'd get an aspx file with the following:


    <% using (Html.BeginForm()) {%>
        <%: Html.ValidationSummary(true) %>

            <div class="editor-label">
                <%: Html.LabelFor(model => model.FirstName) %>

setting dropdownlist item in page load


 I can't get the following to set on page load.  I want the month selected be the current month, on page load.  What needs to change here?

DropDownListCCExpMo.SelectedItem.Selected = Date.Today.Month


Create a year dropdownlist that with dynamically created year values


Using vb.net/asp.net 2005

I have a Year dropdownlist that I want to have the listItems created dynamically with these conditions:

1.  the oldest year selection will be "2009"

2.  all years including "2009" and the current year will be included in the listitems.

It's obvious now (at this date) that 2009 and 2010 will be the 2 current listitems, however I want this dropdownlist to automatically add all subsequent years in coming years so that there will not be the need to add a new year listitem each year.



ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend