1
我已经做了一些经典的ASP,但是对于ASP.NET来说真的很新,所以如果可以的话,请给我一些步骤。asp.net下拉列表代码跨多个页面重用
我试图做的是在不同页面上的多个表单中重复使用相同的下拉列表代码。
我有Form1.aspx页面,它有这样的:
<asp:DropDownList ID="Vendor" runat="server"
AutoPostBack="True"
onselectedindexchanged="ddlVendor_SelectedIndexChanged">
</asp:DropDownList>
然后隐藏文件Form1.aspx.cs页面的代码中有这样一段生成列表的厂商下拉框。
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";
}
}
可以说如果我有另一个form2.aspx页面和form2.aspx.cs页面。此页面将使用与form1.aspx页面相同的下拉列表。我不想在form2.aspx和form2.aspx.cs中重新编写相同的代码。是否有更好的方式在多个页面之间重用这些代码?
由于提前,
将其作为自定义控件进行开发并重用它? – IrishChieftain
是 - 将可重用代码提取到[用户控件](http://msdn.microsoft.com/zh-cn/library/26db8ysc(v = vs.85).aspx) – StuartLC