C# C动态填充的DropDownList在单击按钮时重置

C# C动态填充的DropDownList在单击按钮时重置,c#,asp.net,C#,Asp.net,我有点无聊,需要一些帮助。我有一个DropDownList控件,可以在页面加载时填充值,还有一个submit按钮,可以进行进一步的操作。但是,单击按钮时,DropDownList返回的值为-1,而不是所选选项的值。这里怎么了 网页: <%@ Page Language="C#" MasterPageFile="/new/MasterPageA2.master" EnableEventValidation="false" AutoEventWireup="true" CodeFile="Ch

我有点无聊,需要一些帮助。我有一个DropDownList控件,可以在页面加载时填充值,还有一个submit按钮,可以进行进一步的操作。但是,单击按钮时,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();

}

可以将datasource属性设置为返回数据集的函数的名称。
Try datasource=但您需要更改或重载函数才能将参数更改为字符串,记住设置datavaluefield和datatextfield。或者您可能希望创建一个单独的函数,并在没有参数的情况下调用该函数。

@TimSchmelter如果名称不清楚,我将填充计费国家/地区。根据需要,在这里看一看:由于我没有看到这种行为的明显原因,并且您的页面继承自页面指令中的Common_Checkout inherits=Common_Checkout,最有可能的原因就在这里。我想您已经在较高级别的某个位置关闭了viewstate。1是否启用了viewstate下拉列表?2您是否正在删除代码中任何地方的下拉数据,如果是,则查看它是否被错误执行?3这可能是因为“更新面板”中的某些扭曲内容,请尝试删除“更新面板”,并确认在没有更新面板的情况下是否正常工作,如果正常工作,则至少您会知道“更新面板”正在执行某些操作。@BeenishKhan 1“是”,无论是在页面还是控件级别。2据我所知,上面列出了所有代码。断点显示,只要单击按钮,该值就会被清除。3删除UpdatePanel没有改变任何内容。还有其他想法吗?谢谢。我更新了上面的代码,将发生在PopulateCountry函数调用中的调用包括在内。正如您所看到的,它已经使用了数据绑定。@sbay您是否有用于dropdownlist的onSelectedIndexChanged事件的代码?你能确认回发时dropdownlist没有被重新填充吗?