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 に答える