在单击搜索按钮时通过下拉列表中的第一个值

本文关键字:下拉列表 第一个 单击 搜索 按钮 | 更新日期: 2023-09-27 17:49:37

我有一个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.TextddlCountry.SelectedItem.Value,他们不工作。任何帮助都将非常感激。谢谢你。

在单击搜索按钮时通过下拉列表中的第一个值

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

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

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

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

可以删除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" />

有几种方法可以做到这一点,但我会给你一个简单的答案尝试这个并报告如果这是有效的autoostback设置为false会导致这个问题,viewstate设置为disabled也会导致这个问题

将值存储在ViewState或Session变量中,或者创建一个受保护的属性

假设您正在绑定数据,

有一个DataBound事件,它在数据绑定到dropdown后触发。当你为下拉菜单分配数据源时,你需要在所有绑定到下拉菜单的行之后选择项

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

你也可以得到下拉列表的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.
    }
}

我找到了解决问题的方法。我有一个额外的表单标签在我的主页与method="post"。这导致页面返回到自己。删除表单标记后,我就可以正确地使用下拉列表了。谢谢。