asp.net dropdown list codes reuse across multiple pages

personalloanjj

New Member
I have done some classic ASP, but really new to ASP.NET, so please give me some steps if you can.What I try to do is reuse the same dropdown list codes across multiple forms on different pages. I have the Form1.aspx page, and it has this:\[code\]<asp:DropDownList ID="Vendor" runat="server" AutoPostBack="True" onselectedindexchanged="ddlVendor_SelectedIndexChanged"></asp:DropDownList>\[/code\]Then the code behind file Form1.aspx.cs page has this to build the list for Vendor dropdown box.\[code\]private void FillVendor(){ string strConn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlConnection con = new SqlConnection(strConn); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT VendorID, VendoName FROM Vendor"; DataSet objDs = new DataSet(); SqlDataAdapter dAdapter = new SqlDataAdapter(); dAdapter.SelectCommand = cmd; con.Open(); dAdapter.Fill(objDs); con.Close(); if (objDs.Tables[0].Rows.Count > 0) { Vendor.DataSource = objDs.Tables[0]; Vendor.DataTextField = "VendorName"; Vendor.DataValueField = "VendorID"; Vendor.DataBind(); Vendor.Items.Insert(0, "--Select--"); } else { lblMsg.Text = "No Vendor found"; }}\[/code\]Lets say if I have another form2.aspx page and form2.aspx.cs page. This page will use the same dropdown list like form1.aspx page. I don't want to re-write the same codes again in form2.aspx and form2.aspx.cs. Is there a better way to re-use these codes behind accross multiple pages?Thanks in advance,
 
Back
Top