1

我有一点 DUH 时刻,需要一些帮助。我有一个 DropDownList 控件,它在页面加载时填充了值,还有一个提交按钮可以进行进一步的操作。但是,当单击按钮时,DropDownList 返回值“-1”而不是所选选项。这里有什么问题?

网页:

<%@ Page Language="C#" MasterPageFile="/new/MasterPageA2.master" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Checkout.aspx.cs" Inherits="Common_Checkout" EnableViewState="true" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:UpdatePanel ID="UpdatePanel" runat="server">
<ContentTemplate>

    <asp:Label runat="server" ID="test11"></asp:Label>
    <asp:DropDownList ID="ddlbillingcountry" required="true" EnableViewState="true" runat="server"></asp:DropDownList>
    <asp:Button ID="ImageButton1" AlternateText="Proceed" OnClick="ImageButton1_Click" CausesValidation="false" runat="server" />

</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress ID="UpdateProgress1" runat="server">
    <ProgressTemplate>
        <p>Loading...</p>
    </ProgressTemplate>
</asp:UpdateProgress>
</asp:Content>

后面的代码:

protected void Page_Load(object sender, EventArgs e) {
  if (!IsPostBack) {
    PopulateCountry(ddlbillingcountry);
  }
}

private void PopulateCountry(DropDownList Controlname) {
    DatasetTableAdapters.CountryTableAdapter _CountryTableAdapter = new DatasetTableAdapters.CountryTableAdapter();

    Controlname.ClearSelection();
    Controlname.Items.Clear();      

    DataTable _DataTable = _CountryTableAdapter._Eco_Country_Select(); 
    Controlname.DataSource = _DataTable;
    Controlname.DataTextField = "CountryName";
    Controlname.DataValueField = "Country";
    Controlname.DataBind();
    Controlname.Items.Insert(0, new ListItem("- Select Country -", ""));
}

protected void ImageButton1_Click(object sender, EventArgs e) {
    test11.Text += "<p>clicked " + ddlbillingcountry.SelectedIndex + " - " + ddlbillingcountry.SelectedValue.ToString();

}
4

1 回答 1

0

您可以将 datasource 属性设置为返回数据集的函数的名称。尝试datasource='<%# PopulateCountry("ddlbillingcountry") %>'但您需要更改或重载函数以将参数更改为字符串,记住设置 datavaluefield 和 datatextfield。或者您可能想要创建一个单独的函数并在没有参数的情况下调用它。

于 2012-05-26T03:42:14.700 回答