Asked by:
Dropdown

Question
-
User-1499457942 posted
Hi
I have Months dropdown like below . I want to use it on many forms. Can i declare it at one place and use it everywhere i want.
<asp:DropDownList ID="ddlNMS" runat="server" CssClass="btn btn-default btn-sm">
<asp:ListItem Text="1" Value ="1"/>
<asp:ListItem Text="2" Value ="2"/>
<asp:ListItem Text="3" Value ="3"/>
<asp:ListItem Text="4" Value ="4"/>
<asp:ListItem Text="5" Value ="5"/>
<asp:ListItem Text="6" Value ="6"/>
<asp:ListItem Text="7" Value ="7"/>
<asp:ListItem Text="8" Value ="8"/>
<asp:ListItem Text="9" Value ="9"/>
<asp:ListItem Text="10" Value ="10"/>
<asp:ListItem Text="11" Value ="11"/>
<asp:ListItem Text="12" Value ="12"/>
</asp:DropDownList>Thanks
Saturday, August 11, 2018 2:50 PM
All replies
-
User475983607 posted
User control.Saturday, August 11, 2018 2:58 PM -
User-1716253493 posted
https://msdn.microsoft.com/en-us/library/wt3k2fyw.aspx?f=255&MSPPError=-2147217396
Saturday, August 11, 2018 4:57 PM -
User-893317190 posted
Hi JagjitSingh,
As mgebhard and oned_gk have said ,you could use usercontrol.Uusercontrol is often used to reuse your control.
To use usercontrol, add a new item called Web Forms User Control and add your dropdownlist into your user control’s ascx page.
In the aspx page where you want to reuse the dropdownlist ,you should use Register directive to register your usercontrol. You should specify the Src property of the register directive as the path of your ascx usercontrol. You should also specify the TagPrefix and TagName property of the register directive.
Below is my code.
My usercontrol.
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="DropDownUserControl.ascx.cs" Inherits="MyWebFormCases.ExtraControls.DropDownUserControl" %> <asp:DropDownList ID="ddlNMS" runat="server" CssClass="btn btn-default btn-sm"> <asp:ListItem Text="1" Value ="1"/> <asp:ListItem Text="2" Value ="2"/> <asp:ListItem Text="3" Value ="3"/> <asp:ListItem Text="4" Value ="4"/> <asp:ListItem Text="5" Value ="5"/> <asp:ListItem Text="6" Value ="6"/> <asp:ListItem Text="7" Value ="7"/> <asp:ListItem Text="8" Value ="8"/> <asp:ListItem Text="9" Value ="9"/> <asp:ListItem Text="10" Value ="10"/> <asp:ListItem Text="11" Value ="11"/> <asp:ListItem Text="12" Value ="12"/> </asp:DropDownList>
My aspx page.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UseDropDownUserControl.aspx.cs" Inherits="MyWebFormCases.ExtraControls.UseDropDownUserControl" %> <%@ Register Src="~/ExtraControls/DropDownUserControl.ascx" TagPrefix="uc1" TagName="DropDown" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> </head> <body> <form id="form1" runat="server"> <uc1:DropDown runat="server" ID="kkk" ></uc1:DropDown> </form> </body> </html>
Code behind.
protected void Page_Load(object sender, EventArgs e) { DropDownList dropDownList= kkk.FindControl("ddlNMS") as DropDownList; ListItemCollection collection= dropDownList.Items; }
The result.
Best regards ,
Ackerly Xu
Monday, August 13, 2018 8:00 AM -
User-1171043462 posted
You can make it a UserControl.
Check this example
Select Day, Month and Year Date from DropDownList in ASP.Net
Monday, August 13, 2018 8:57 AM -
User-1320437544 posted
100% accord to the rest opinions you should use user control. Google it there are many tutotials.
Tuesday, August 14, 2018 4:01 PM