Click here to Skip to main content
15,895,142 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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

A simplest way is

modify query as below
SQL
select 0 as id, 'Create New' as name
union all
select id,name from tblnm

Happy Coding!
:)
 
Share this answer
 
XML
<asp:DropDownList ID="DropDownList1" AppendDataBoundItems="true" runat="server" DataSourceID="SqlDataSource1" DataTextField="state" DataValueField="state">

    <asp:ListItem Text="(Select a State)" Value="" />

</asp:DropDownList>
 
Share this answer
 
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
 
Share this answer
 
v2
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
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900