Click here to Skip to main content
Sign Up to vote bad
good
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:41


4 solutions

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  
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">
 
    <asp:ListItem Text="(Select a State)" Value="" />
 
</asp:DropDownList>
  Permalink  
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  
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  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Mohammed Hameed 268
1 OriginalGriff 261
2 Sergey Alexandrovich Kryukov 188
3 Mayur_Panchal 153
4 Santhosh G_ 108
0 Sergey Alexandrovich Kryukov 8,216
1 OriginalGriff 6,271
2 CPallini 3,528
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid