当 ASP.NET DropDownList.SelectedValue 位于 FormView 编辑项模板内时,如何设置它?

发布于 2024-10-02 18:30:46 字数 1312 浏览 8 评论 0原文

我正在尝试设置位于 FormView 的编辑模板内的 DropDownList 的选定值。每当我访问它时,我都会收到错误:

对象引用不存在

我尝试通过以下方式设置它:

DropDownList ddl = (DropDownList)FormView1.FindControl("ddlFrequency");
ddl.SelectedValue = "blah blah";

并且还喜欢:

((DropDownList)FormView1.FindControl("ddlFrequency")).SelectedValue = "blah blah";

如何设置此 DropDownList.SelectedValue?

编辑:这是整个方法:

  protected void btnEdit_Click(object sender, EventArgs e)
{
    String frequency = ((Label)(FormView1.FindControl("lblFrequency"))).Text;

    FormView1.ChangeMode(FormViewMode.Edit);

    String selectedValue = "0";

    switch (frequency.ToLower())
    {
        case "none": selectedValue = "0"; break;
        case "daily": selectedValue = "1"; break;
        case "weekly": selectedValue = "7"; break;
        case "monthly": selectedValue = "28"; break;
        case "bi-monthly": selectedValue = "56"; break;
        case "quarterly": selectedValue = "84"; break;
        case "semi-annually": selectedValue = "168"; break;
        case "annually": selectedValue = "365"; break;
        default: break;
    }

    DropDownList ddl = (DropDownList)FormView1.FindControl("ddlFrequency");
    ddl.SelectedValue = selectedValue;
}

I'm trying to set the selected value of a DropDownList which sits inside the Edit template of a FormView. Whenever I access it, I get error:

Object reference doesn't exist

I'm trying to set it in the following ways:

DropDownList ddl = (DropDownList)FormView1.FindControl("ddlFrequency");
ddl.SelectedValue = "blah blah";

And also like:

((DropDownList)FormView1.FindControl("ddlFrequency")).SelectedValue = "blah blah";

How can I set this DropDownList.SelectedValue?

EDIT: Here is the entire method:

  protected void btnEdit_Click(object sender, EventArgs e)
{
    String frequency = ((Label)(FormView1.FindControl("lblFrequency"))).Text;

    FormView1.ChangeMode(FormViewMode.Edit);

    String selectedValue = "0";

    switch (frequency.ToLower())
    {
        case "none": selectedValue = "0"; break;
        case "daily": selectedValue = "1"; break;
        case "weekly": selectedValue = "7"; break;
        case "monthly": selectedValue = "28"; break;
        case "bi-monthly": selectedValue = "56"; break;
        case "quarterly": selectedValue = "84"; break;
        case "semi-annually": selectedValue = "168"; break;
        case "annually": selectedValue = "365"; break;
        default: break;
    }

    DropDownList ddl = (DropDownList)FormView1.FindControl("ddlFrequency");
    ddl.SelectedValue = selectedValue;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

榆西 2024-10-09 18:30:47

那么编辑模板必须可见才能让 FindControl 工作。您可能必须使用 OnModeChanged 事件来检查编辑模式,然后找到 DropDownList。

Well the edit template must be visible for FindControl to work. You will probably have to use the OnModeChanged event to check for the Edit mode and then find the DropDownList.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文