2

我有一个 asp 按钮和四个从数据库动态填充的下拉列表。单击按钮时,应该将下拉列表中的选定值传递给 SQL 存储过程。相反,每个下拉列表中的第一个值被传递而不是选定的值。这是aspx页面上的代码:

<asp:DropDownList runat="server" ID="ddlCountry" AppendDataBoundItems="True">
</asp:DropDownList>
&nbsp;
<asp:DropDownList runat="server" ID="ddlTypeStructure" AppendDataBoundItems="True">
 </asp:DropDownList>
&nbsp;
<asp:DropDownList runat="server" ID="ddlCategory" AppendDataBoundItems="True">
</asp:DropDownList>
&nbsp;
<asp:DropDownList runat="server" ID="ddlSegment" AppendDataBoundItems="True">
</asp:DropDownList>
&nbsp;
<asp:Button runat="server" ID="btnSearhProjects" Text="Go" 
    CausesValidation="False" onclick="btnSearhProjects_Click" UseSubmitBehavior="False" />

这是按钮背后的 C# 代码:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCountries();
            BindCategories();
            BindSegments();
            BindStructures();

        }



    }
public void SearchProjects()
    {
        Project project = new Project();
        string country = ddlCountry.SelectedItem.Text;
        string category = ddlCategory.SelectedItem.Text;
        string segment = ddlSegment.SelectedItem.Text;
        string structure = ddlTypeStructure.SelectedItem.Text;
        List<Project> projectList = project.GetProjects(country, category, segment, structure);
        gvProjects.DataSource = projectList;
        gvProjects.DataBind();
    }

    protected void btnSearhProjects_Click(object sender, EventArgs e)
    {
        SearchProjects();
    }

这就是我填充每个下拉列表的方式:

public void BindCountries()
    {
        List<Country> countryList = new List<Country>();
        Country country = new Country();
        countryList = country.GetCountries();

        ddlCountry.DataTextField = "CountryName";
        ddlCountry.DataValueField = "CountryID";
        ddlCountry.DataSource = countryList;
        ddlCountry.DataBind();

    }

我尝试过使用ddlCountry.SelectedValue.Text并且ddlCountry.SelectedItem.Value它们不起作用。任何帮助将不胜感激。谢谢你。

4

5 回答 5

1

if (!IsPostBack)在事件的代码块中设置断点(或一些调试语句),Page_Load以确保它不会在按钮单击时执行。

还要确保valueDropDownList ListItems 的属性都是唯一的,否则会将其设置为第一个重复值。

于 2012-12-27T20:26:24.607 回答
0

问题是页面加载没有运行——因为这些是动态创建的,它们没有在回发时设置。

您也可以将这些值存储在视图状态中,但您似乎已关闭页面的视图状态。(视图状态在动态页面上可能是一个问题,但是您需要解决这个问题。)

于 2012-12-27T20:28:28.390 回答
0

有几种方法可以做到这一点,但我会让你给你一个简单的答案试试这个并报告如果这有效 Autopostback 设置为 false 可能会导致这种情况,禁用视图状态也会导致这种情况

将值存储在 ViewState 或 Session 变量中,或者根据需要创建受保护的属性

假设您正在绑定数据,

有 DataBound 事件,它在数据绑定到下拉列表后触发。当您将 dataSource 分配给您的 dropwdwon 时,您需要在所有绑定到下拉列表的行之后选择项目

var selectedValue string.Empty;
protected void DropDownList1_DataBound(object sender, EventArgs e)
{
   selectedValue = ddlCountry.SelectedValue; // store it in some variable
}

您还可以获取 DropDownList 的 ListItem 并使用 foreach 来查看选择了哪些项目。请确保您在 Page_Load 的 if (IsPostBack) 中执行此操作,记住按钮单击会触发 PostBack

foreach (ListItem item in ddlCountry.Items)
{
    if (item.Selected)
    {
        // TODO: Whatever you are doing with a selected item.
    }
}
于 2012-12-27T20:30:26.733 回答
0

您可以删除 AppendDataBoundItems="true"。

DropDownList AppendDataBoundItems(第一项为空白且无重复项)

<asp:DropDownList runat="server" ID="ddlCountry">
</asp:DropDownList>
&nbsp;
<asp:DropDownList runat="server" ID="ddlTypeStructure">
 </asp:DropDownList>
&nbsp;
<asp:DropDownList runat="server" ID="ddlCategory">
</asp:DropDownList>
&nbsp;
<asp:DropDownList runat="server" ID="ddlSegment">
</asp:DropDownList>
&nbsp;
<asp:Button runat="server" ID="btnSearhProjects" Text="Go" 
    CausesValidation="False" onclick="btnSearhProjects_Click" UseSubmitBehavior="False" />
于 2012-12-27T20:34:03.173 回答
0

我找到了解决我的问题的方法。我的母版页上有一个额外的表单标签,带有method="post". 这导致页面回发到自身。删除表单标签后,我就可以正确使用下拉列表了。谢谢。

于 2012-12-28T15:54:12.013 回答