如何使用 DAL 在业务对象类定义中实现泛型以创建正确的用户控件下拉列表

发布于 2024-12-13 18:50:01 字数 2785 浏览 0 评论 0原文

不幸的是,我对仿制药还很陌生,因为它依赖于企业内部网 Web 应用程序的支持,而该应用程序的升级过程必然受到繁文缛节和缓慢变化的标准的约束。因此,今天(值得庆幸的是!)我终于发现自己在升级到 .Net 3.5 并将所有代码转换为正确的分层模型期间陷入了混乱。

我整个上午都在阅读有关泛型的内容,试图消化如何将下拉用户控件转换为适当的业务对象,该对象从数据访问层中的类获取其数据。

这里有一个非常简洁的问题,详细说明了我有兴趣探索的内容: 在用户控件的下拉列表中设置选定的索引

然而,我很想看到的是 Travel_CarSizes.GetCarSizes() 内部的实际外观以及类 Travel_CarSizes 的定义方式。 (我很难使用 并知道它应该发生在哪里。)

对于我自己目前的具体情况,我需要一个下拉用户控件来包含位置方向(N,S, W、C/O、NW、SE 等)存储在数据库中的 SQL 表中,当表单数据存在时,其选定的索引需要能够由它所在的任何页面来设置。

我已经开始从上面的链接实现示例中的模型,但现在不使用泛型,因为我无法弄清楚:

下拉用户控件:

public partial class DropDownStreetPrefix : System.Web.UI.UserControl
{
    public string StreetPrefixValue
    {
        get { return ddlStreetPrefix.SelectedValue.ToString(); }
        set
        {
            Bind();
            ddlStreetPrefix.SelectedIndex = ddlStreetPrefix.Items.IndexOf(ddlStreetPrefix.Items.FindByValue(value));
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();             
        }

    }

    private void Bind()
    {
        if (ddlStreetPrefix.Items.Count == 0)
        {
            SqlDataReader rdr = StreetDirectionals.GetDirectionals();
            ddlStreetPrefix.DataSource = rdr;
            ddlStreetPrefix.DataBind();

            ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr";
            ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr";

            ListItem li = new ListItem("", "");
            ddlStreetPrefix.Items.Insert(0, li);
            ddlStreetPrefix.SelectedIndex = 0;
        }
    }
}

StreetDirectionals类:

public class StreetDirectionals
{
    private StreetDirectionals () { }

    public static SqlDataReader GetDirectionals ()
    {
        string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " +
            "ORDER BY StreetSuffixPrefixAbbr";

        SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText);

        return rdr;
    }

}

如果我要将下拉用户控件中的 Bind() 方法转换为以下内容,一旦我弄清楚如何更改其代码,我就会将 StreetDirectionals 类中的数据库交互分离出来:

private void Bind()
{
    if (!IsPostBack)
    {
        **List<StreetDirectionals> sd = StreetDirectionals.GetDirectionals();**
        ddlStreetPrefix.DataSource = sd;
        ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr";
        ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr";
        ddlStreetPrefix.DataBind();
    }
}

任何帮助都会非常感谢!

I am woefully new to generics, being tied to the support of a corporate intranet web application whose upgrade process is bound to red tape and slowwwly-changing standards. Consequently, today (thankfully!) I finally find myself scrambling during our upgrade to .Net 3.5 and transitioning all the code I can to a properly tiered model.

I have been reading all morning about generics trying to digest how to transition dropdown user controls into a proper business object that gets its data from a class in the data access layer.

There is a perfectly succinct question here that details exactly what I am interested in exploring: Set selected index in a Dropdownlist in usercontrol.

What I would love to see, however, is what Travel_CarSizes.GetCarSizes() actually looks like inside and how the class Travel_CarSizes is defined. (I am having a hard time with <T> and knowing where it should occur.)

For my own specific circumstance at the moment I need a dropdown user control to contain location directionals (N, S, W, C/O, NW, SE, etc) that are stored in a SQL table in the DB and whose selected index needs to be able to be set by whichever page it happens to be in, when form data exists.

I have begun to implement the model in the example from the link above but right now without using Generics because I can't figure it out:

The dropdown user control:

public partial class DropDownStreetPrefix : System.Web.UI.UserControl
{
    public string StreetPrefixValue
    {
        get { return ddlStreetPrefix.SelectedValue.ToString(); }
        set
        {
            Bind();
            ddlStreetPrefix.SelectedIndex = ddlStreetPrefix.Items.IndexOf(ddlStreetPrefix.Items.FindByValue(value));
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Bind();             
        }

    }

    private void Bind()
    {
        if (ddlStreetPrefix.Items.Count == 0)
        {
            SqlDataReader rdr = StreetDirectionals.GetDirectionals();
            ddlStreetPrefix.DataSource = rdr;
            ddlStreetPrefix.DataBind();

            ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr";
            ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr";

            ListItem li = new ListItem("", "");
            ddlStreetPrefix.Items.Insert(0, li);
            ddlStreetPrefix.SelectedIndex = 0;
        }
    }
}

The StreetDirectionals class:

public class StreetDirectionals
{
    private StreetDirectionals () { }

    public static SqlDataReader GetDirectionals ()
    {
        string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " +
            "ORDER BY StreetSuffixPrefixAbbr";

        SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText);

        return rdr;
    }

}

I will separate out the database interaction inside the StreetDirectionals class as soon as I can figure out how to change its code if I were to transform the Bind() method from my dropdown user control into this:

private void Bind()
{
    if (!IsPostBack)
    {
        **List<StreetDirectionals> sd = StreetDirectionals.GetDirectionals();**
        ddlStreetPrefix.DataSource = sd;
        ddlStreetPrefix.DataTextField = "StreetSuffixPrefixAbbr";
        ddlStreetPrefix.DataValueField = "StreetSuffixPrefixAbbr";
        ddlStreetPrefix.DataBind();
    }
}

Any assistance would be sooo much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏天碎花小短裙 2024-12-20 18:50:01
public class StreetDirectional
{
    public string StreetSuffixPrefixAbbr { get; set; }

    public static IEnumerable<StreetDirectional> GetDirectionals ()
    {
        string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " 
            + "ORDER BY StreetSuffixPrefixAbbr";

        SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText);

        var list = new List<StreetDirectional>();

        while (rdr.Read())
        {
            var item = new StreetDirectional() { StreetSuffixPrefixAbbr = (string)rdr["StreetSuffixPrefixAbbr"] };
            list.Add(item);
        }

        return list;
    }

}

那么你可以这样做

ddlStreetPrefix.DataSource = StreetDirectional.GetDirectionals();
public class StreetDirectional
{
    public string StreetSuffixPrefixAbbr { get; set; }

    public static IEnumerable<StreetDirectional> GetDirectionals ()
    {
        string sqlText = "SELECT StreetSuffixPrefixAbbr FROM common..tblStreetSuffixPrefix " 
            + "ORDER BY StreetSuffixPrefixAbbr";

        SqlDataReader rdr = SqlClient.ExecuteFetchReturnDataReader( theConnectionString, CommandType.Text, sqlText);

        var list = new List<StreetDirectional>();

        while (rdr.Read())
        {
            var item = new StreetDirectional() { StreetSuffixPrefixAbbr = (string)rdr["StreetSuffixPrefixAbbr"] };
            list.Add(item);
        }

        return list;
    }

}

then you can do this

ddlStreetPrefix.DataSource = StreetDirectional.GetDirectionals();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文