单击按钮时的下拉列表默认值

本文关键字:下拉列表 默认值 按钮 单击 | 更新日期: 2023-09-27 17:57:57

我正在做一个asp项目。我有一种情况,我需要搜索文件并显示在gridview中。网格视图有三个下拉列表,我现在的问题是如何根据单击搜索按钮时返回的结果集显示下拉列表的默认值,因为我已经在数据行绑定上设置了下拉列表的缺省值("请选择")。因为在第一次加载时,我的下拉菜单应该显示"请选择"值。非常感谢你的帮助。下面是我的代码。

 protected void btnSearch_Click(object sender, EventArgs e)
{
    int uFlag = 0;
    string uploadFlag = this.ddlUploadDate.SelectedValue;
    string fileName = this.txtSearchText.Text;
    string uploadDt = this.txtDate.Text;
    string status = this.ddlStatus.SelectedValue.ToString();
    List<EventFile> fileSearch = new List<EventFile>();
    fileSearch = CoMailAssociationDAL.SearchFile(uFlag, fileName, uploadDt, status);
    gvwAssociation.DataSource = fileSearch;
    gvwAssociation.DataBind();
}
 protected void gvwAssociation_RowDataBound(object sender, GridViewRowEventArgs e)
{
    ListItem Item = new ListItem();
    Item.Text = "Please Select";
    Item.Value = "0";
    Item.Selected = true;
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        DropDownList ddlpool = (DropDownList)e.Row.FindControl("ddlpool");
        DropDownList ddlyear = (DropDownList)e.Row.FindControl("ddlyear");
        DropDownList ddlevent = (DropDownList)e.Row.FindControl("ddlevent");
        ddlpool.DataSource = CoMailAssociationDAL.GetCoBindEvents();
        ddlpool.DataBind();
        ddlpool.Items.Insert(0, Item);
        ddlevent.DataSource = CoMailAssociationDAL.GetCoBindEvents();
        ddlevent.DataBind();
        ddlevent.Items.Insert(0, Item);
        for (int intCount = 2013; intCount <= 2020; intCount++)
        {
            ddlyear.Items.Add(intCount.ToString());   
            ddlyear.SelectedIndex = 1;             
        }
    }
}

单击按钮时的下拉列表默认值

您可以在网格RowDataBound事件中创建一个条件,方法是在itemtemplate中为数据库中的值获取一个隐藏字段,并将其作为

for (int intCount = 2013; intCount <= 2020; intCount++)
    {
        ddlyear.Items.Add(intCount.ToString());   
        HiddenField result= GridView1.Rows[e.RowIndex].FindControl("hdnpool") as HiddenField;
        if(result!=null)
         ddlyear.SelectedIndex =result.value;
        else
         ddlyear.SelectedIndex = 1;             
    }

在网格中,请为每个下拉列表使用以下内容:

  <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddpool" runat="server"></asp:DropDownList>
                        <asp:HiddenField ID="hdnpool" Value="<%# Eval("PoolColumninDB") %>" runat="server"></asp:HiddenField>
                    </ItemTemplate>
                </asp:TemplateField>

你试过这个吗

for (int intCount = 2013; intCount <= 2020; intCount++)
    {
        ddlyear.Items.Add(intCount.ToString());   
        ddlyear.SelectedValue= 1;             
    }