【发布时间】:2016-11-15 03:24:24
【问题描述】:
在我的 asp 页面中,我有一个下拉列表,其值正在从数据库中检索。为了检索下拉列表的值,我在页面后面的代码中编写了一个方法。
现在我还要在另一个 asp 页面中使用相同的下拉菜单。为此,我正在将相同的方法写入相应的后台代码以从数据库中检索值。
我想知道有什么方法可以让我重用代码隐藏页面中所需的方法吗?
例如。产品页面(asp page)
<tr>
<td class="va-top">Type:</td>
<td><asp:ListBox ID="listBox_ProductType" runat="server" Rows="1" Width="300px"></asp:ListBox></td>
</tr>
aspx 页面
public void GetProductBillingType()
{
try
{
DataTable dt = new DataTable();
listBox_ProductType.ClearSelection();
DAL_Product_Registration objDAL = new DAL_Product_Registration();
dt = objDAL.Get_ProductBillingType();
if (dt != null && dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
listBox_ProductType.Items.Add(new ListItem(row["billing_sub_type"].ToString(), row["billing_dtls_id"].ToString()));
}
}
}
catch (Exception ex) { }
}
现在在另一个页面中,我必须使用相同的下拉菜单。我也在页面后面的另一个代码中编写相同的方法。
但是有什么方法可以重用 aspx 页面中使用的方法。
【问题讨论】:
-
您可以将方法定义为静态类中的静态方法,并可以从那里调用该方法。有意义吗?
-
@lukai 感谢您的回答。但是请您用一些代码示例来解释您的回复。这会很有帮助。谢谢!