从asp.net中的下拉列表中获取选定的值

发布于 2024-10-03 18:50:32 字数 1134 浏览 2 评论 0原文

我有一个下拉列表,显示 aspx 文件中数据库中的国家/地区列表,

public void ShowCountries()
    {
        OdbcConnection conn;
        conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["jConnString"].ConnectionString);
        conn.Open();
        string sql = "SELECT iso,printable_name FROM country";

        OdbcCommand cmd = new OdbcCommand(sql, conn);


        try
        {
            //ddlCountry.DataSourceID = "country";
            ddlCountry.DataSource = cmd.ExecuteReader();
            ddlCountry.DataTextField = "printable_name";
            ddlCountry.DataValueField = "iso";
            ddlCountry.DataBind();
        }
        catch (Exception ex)
        {
            Check.Text = "3" + ex.Message;
        }
        finally
        {
            ddlCountry.Dispose();
            conn.Close();
            conn.Dispose();
        }

    }

这就是我调用此数据绑定列表的方式。

    <asp:DropDownList ID="ddlCountry" runat="server" 
DataTextField="printable_name" 
DataValueField="iso">
    </asp:DropDownList>

它显示列表,但如果我想选择第一个选项以外的选项,它总是插入值第一个选项从来没有选择过,我做错了什么?

I have a dropdownlist which shows a list of countries from my database

public void ShowCountries()
    {
        OdbcConnection conn;
        conn = new OdbcConnection(ConfigurationManager.ConnectionStrings["jConnString"].ConnectionString);
        conn.Open();
        string sql = "SELECT iso,printable_name FROM country";

        OdbcCommand cmd = new OdbcCommand(sql, conn);


        try
        {
            //ddlCountry.DataSourceID = "country";
            ddlCountry.DataSource = cmd.ExecuteReader();
            ddlCountry.DataTextField = "printable_name";
            ddlCountry.DataValueField = "iso";
            ddlCountry.DataBind();
        }
        catch (Exception ex)
        {
            Check.Text = "3" + ex.Message;
        }
        finally
        {
            ddlCountry.Dispose();
            conn.Close();
            conn.Dispose();
        }

    }

in the aspx file this is the way how I call this databounded list

    <asp:DropDownList ID="ddlCountry" runat="server" 
DataTextField="printable_name" 
DataValueField="iso">
    </asp:DropDownList>

It shows the list but if I want to select an option other then the first one it always inserts the value of the first option en never the selected one, what am I doing wrong ?

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

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

发布评论

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

评论(1

南城追梦 2024-10-10 18:50:32

在我看来,在访问所选值之前,数据源正在(重新)绑定到控件,因此所选值始终是数据源中的第一个值。

ShowCountries 在何时何地被调用?据猜测,我想说您在 Page_Load 中缺少对 IsPostback 的检查

if (!IsPostback) {
    // bind the datasource here, when the page initially loads
    ShowCountries();
}

另外,我认为您不想调用 Dispose()< /code> 位于 finally 块中的 ddlCountry 上。

It sounds to me like the data source is being (re-)bound to the control before you are accessing the selected value, thus the selected value is always the first value in the datasource.

Where and when does ShowCountries get called? At a guess, I'd say you're missing a check for IsPostback in Page_Load

if (!IsPostback) {
    // bind the datasource here, when the page initially loads
    ShowCountries();
}

Also, I don't think you want to call Dispose() on ddlCountry in the finally block.

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