我正在开发一个asp项目。我有一种情况需要搜索文件并在gridview中显示。 gridview有三个下拉列表,我现在的问题是如何根据我点击搜索按钮时返回的结果集显示我的下拉列表的默认值,因为我已经设置了下拉列表的默认值(&#34 ;请在数据行绑定上选择")。因为在第一次加载时我的下拉列表应显示"请选择"值。非常感谢您的帮助。下面是我的代码。
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;
}
}
}
答案 0 :(得分:1)
您可以通过在itemtemplate中为数据库中的一个隐藏字段创建一个条件来创建条件RowDataBound事件,并将其作为
找到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>
答案 1 :(得分:-2)
你试试这个吗?
for (int intCount = 2013; intCount <= 2020; intCount++)
{
ddlyear.Items.Add(intCount.ToString());
ddlyear.SelectedValue= 1;
}