Click here to Skip to main content
Rate this: bad
good
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 0: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
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 268
3 Sergey Alexandrovich Kryukov 263
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web01 | 2.8.140827.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
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