我在formview中有一个编辑按钮和一个下拉列表。我正在使用Linq To Entities来获取我需要使用的数据,并且填充和查看formview itemtemplate没有问题。
但是,下拉列表控件(id="ddlEligibility"
)仅在edititemtemplate
中(我使用itemtemplate
中的文本框来显示当前值)并且我遇到问题最初显示在itemtemplate中的值,以便在显示edititemtemplate时显示。我现在得到的是显示第一个值的下拉列表。我希望itemtemplate中的值默认显示,用户可以根据需要更改它。以下是填充下拉列表的代码。有人有建议吗?
protected void btnEdit_Click(object sender, EventArgs e)
{
fvSubscriber.ChangeMode(FormViewMode.Edit);
fvSubscriber.DataBind();
LifeLineDSEntities context = new LifeLineDSEntities():
var program = from p in context.EligibilityPrograms
select p;
DropDownList ddlEligibility = (DropDownList)(fvSubscriber.FindControl("ddlEligibility")));
if (ddlEligibility != null)
{
ddlEligibility.DataSource = program;
ddlEligibility.DataTextField = "ProgramName";
ddlEligibility.DataValueField = "eligibilityCode";
ddlEligibility.DataBind();
}
}
FormView中的DropDownlist ...
<form id="form1" runat="server">
<asp:FormView ID="fvSubscriber" runat="server" RenderOuterTable="false" DefaultMode="Readonly" OnModeChanging="fvSubscriberChanging">
<ItemTemplate>
<asp:TextBox ID="txtEligibility" runat="server" Text='<%# Eval("ProgramName") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlEligibility" runat="server" />
</EditItemTemplate>
</asp:FormView>
</form>
答案 0 :(得分:0)
您可以在更改formview模式之前尝试获取文本框的值。
TextBox txtEligibility= (TextBox)fvSubscriber.FindControl("txtEligibility");
fvSubscriber.ChangeMode(FormViewMode.Edit);
fvSubscriber.DataBind();
LifeLineDSEntities context = new LifeLineDSEntities():
var program = from p in context.EligibilityPrograms
select p;
DropDownList ddlEligibility = (DropDownList)(fvSubscriber.FindControl("ddlEligibility")));
if (ddlEligibility != null)
{
ddlEligibility.DataSource = program;
ddlEligibility.DataTextField = "ProgramName";
ddlEligibility.DataValueField = "eligibilityCode";
ddlEligibility.DataBind();
if (txtEligibility != null)
{
if(!string.IsNullOrWhiteSpace(txtEligibility.Text))
{
foreach (ListItem item in ddlEligibility.Items)
{
if (item.Text == txtEligibility.Text)
{
ddlEligibility.SelectedValue = txtEligibility.Text;
}
}
}
}
}