0

我在表单视图中有一个编辑按钮和一个下拉列表。我正在使用 Linq To Entities 来获取我需要使用的数据,并且在填充和查看 formview itemtemplate 时没有问题。

但是,下拉列表控件id="ddlEligibility"(我现在得到的只是显示第一个值的下拉列表。我希望 itemtemplate 中的值默认显示,并且用户可以根据需要更改它。这是填充下拉列表的代码。有人有建议吗?edititemtemplateitemtemplate

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 中的下拉列表...

<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>
4

1 回答 1

0

在更改表单视图模式之前,您可以尝试获取文本框的值。

    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;
                    }
                }
            }
        }
    }
于 2013-11-14T12:31:45.597 回答