Click here to Skip to main content
12,242,492 members (58,552 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
I have two asp.net webpages:
1.Form.aspx
2.Subform.aspx


->I have a dropdownlist bound to a sql datasource.when i run the form.aspx,the dropdownlist successfully shows all the names from the database.

->But what i want My dropdownlist to also show an option "create new" along with the database records.
So that when i select "create new" from the dropdownlist, the second webpage i.e.Subform.aspx gets open.
Posted 27-Dec-12 1:41am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

as you said to show,
"create new" along with the database records.

A simplest way is

modify query as below
select 0 as id, 'Create New' as name
union all
select id,name from tblnm
Happy Coding!
Smile | :)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">
 
    <asp:ListItem Text="(Select a State)" Value="" />
 
</asp:DropDownList>
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

The Dropdownlist Items can be added through codebehind...But, one main point is

you cant add the items in page load.

Ex: ddl_type.Items.Insert(0,"Create New");

It can be added on Page_PreRenderComplete stage of the Page Life Cycle.

The Items Will Be Bounded in this stage of Page Life Cycle.

Reason: The Page_PreRenderComplete() here the datasource id will be appended to the required controls.

For Detailed Description Go through this link.
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx[^]

To Open new page Enable the Autopostback = true and in code behind on dropdownlist selected index change.

All The Best
With Regards
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Just Do the follow:

this will be on page load of the page :
 if(!ispostback)
 {
 
  DataSet ds = new DataSet();
  SqlDataAdapter myda = new SqlDataAdapter("select field name from table", connection object);
  myda.Fill(ds);
  drop_down1.DataSource = ds;
  drop_down1.DataValueField = "field name";
  drop_down1.DataBind();
  drop_down1.Items.Insert(0, new ListItem("Create New", "0"));
}

if any problem, please post it.


Mitesh
  Permalink  
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160426.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100