Databound DropDownList默认选项始终返回0

时间:2021-09-22 09:46:16

I am creating a form to upload images. I have a dropdownlist fed by a SQL database, which contains the name and Id of previously created albums that the user may upload images into. If I select the second or lower option in the dropdownlist it always returns the correct Id, but if I leave the default option selected it always returns 0.

我正在创建一个表单来上传图像。我有一个由SQL数据库提供的下拉列表,其中包含用户可以将图像上传到的先前创建的相册的名称和ID。如果我在下拉列表中选择第二个或更低的选项,它总是返回正确的Id,但是如果我选择默认选项,它总是返回0。

I feel like I'm missing something obvious here!

我觉得我在这里遗漏了一些明显的东西!

On page load:

在页面加载:

    protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        if (Session["userId"] == null)
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            BindDropDownList();
        }
    }

BindDropDownList method:

    string qry2 = "SELECT Id, albumName FROM albums WHERE albumOwnerId=@albOwnId";

    SqlCommand cmd2 = new SqlCommand(qry2, conn);

    cmd2.Parameters.AddWithValue("@albOwnId", Session["userId"]);

    SqlDataAdapter da2 = new SqlDataAdapter(cmd2);

    DataTable dt2 = new DataTable();

    da2.Fill(dt2);

    album.DataValueField = "Id";
    album.DataTextField = "albumName";

    album.DataSource = dt2;
    album.DataBind();

    album.SelectedItem.Value = dt2.Rows[0]["Id"].ToString();
    albumData = Int32.Parse(album.SelectedItem.Value);

    conn.Close();
}

Selected Index Changed Method:

选择的索引更改方法:

     protected void album_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList album = Page.FindControl("album") as DropDownList;

    albumData = Int32.Parse(album.SelectedItem.Value);
}

ASP.net:

<asp:DropDownList ID="album" runat="server" OnSelectedIndexChanged="album_SelectedIndexChanged">
    </asp:DropDownList><br />

1 个解决方案

#1


After the discussion, I think the problem is that you handle the OnSelectedIndexChanged event and not the OnClick event of the button.

讨论之后,我认为问题在于您处理OnSelectedIndexChanged事件而不是按钮的OnClick事件。

If you don't change the value of the dropdownlist the OnSelectedIndexChanged will not be raised.

如果不更改下拉列表的值,则不会引发OnSelectedIndexChanged。

You should handle the button OnClick event:

您应该处理按钮OnClick事件:

<asp:Button ID="btn" runat="server" Text="Go" OnClick="btn_Click" />

protected void btn_Click(object sender, EventArgs e)
{
    DropDownList album = Page.FindControl("album") as DropDownList;

    albumData = Int32.Parse(album.SelectedItem.Value);
}

#1


After the discussion, I think the problem is that you handle the OnSelectedIndexChanged event and not the OnClick event of the button.

讨论之后,我认为问题在于您处理OnSelectedIndexChanged事件而不是按钮的OnClick事件。

If you don't change the value of the dropdownlist the OnSelectedIndexChanged will not be raised.

如果不更改下拉列表的值,则不会引发OnSelectedIndexChanged。

You should handle the button OnClick event:

您应该处理按钮OnClick事件:

<asp:Button ID="btn" runat="server" Text="Go" OnClick="btn_Click" />

protected void btn_Click(object sender, EventArgs e)
{
    DropDownList album = Page.FindControl("album") as DropDownList;

    albumData = Int32.Parse(album.SelectedItem.Value);
}