如何进入FormView的编辑模式?

发布于 2024-11-02 17:21:26 字数 764 浏览 6 评论 0原文

我有 FormView:

<asp:FormView ID="fvReport" runat="server" DefaultMode="ReadOnly" AllowPaging="false" OnModeChanging="fvReport_ModeChanging" DataKeyNames="id">

protected void fvReport_ModeChanging(Object sender, FormViewModeEventArgs e)
    {
        switch (e.NewMode)
        {
            case FormViewMode.Edit:
                fvReport.AllowPaging = false;
                break;
        }
    }

在 ItemTamplate 中我放置了 LinkBut​​ton:

<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="true" CommandName="Edit" CommandArgument='<%# Eval("id") %>'>Редактировать</asp:LinkButton>

当然,FormView 有 EditItemTemplate 部分。

当我单击按钮时,FormView 会刷新并保持只读状态。我做错了什么?

I have FormView:

<asp:FormView ID="fvReport" runat="server" DefaultMode="ReadOnly" AllowPaging="false" OnModeChanging="fvReport_ModeChanging" DataKeyNames="id">

protected void fvReport_ModeChanging(Object sender, FormViewModeEventArgs e)
    {
        switch (e.NewMode)
        {
            case FormViewMode.Edit:
                fvReport.AllowPaging = false;
                break;
        }
    }

in ItemTamplate I put LinkButton:

<asp:LinkButton ID="lbEdit" runat="server" CausesValidation="true" CommandName="Edit" CommandArgument='<%# Eval("id") %>'>Редактировать</asp:LinkButton>

Of course, FormView has EditItemTemplate section.

When I click Button, FormView is refreshed and stays in ReadOnly. What am I doing wrong?

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

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

发布评论

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

评论(3

翻了热茶 2024-11-09 17:21:26

您必须调用 FormView 的 ChangeMode 方法并传递模式

fvReport.ChangeMode(DetailsViewMode.Edit);

you have to call ChangeMode method of FormView and pass the mode

fvReport.ChangeMode(DetailsViewMode.Edit);
梦旅人picnic 2024-11-09 17:21:26

我通常用来从 formView 进入编辑模式的另一个选项是添加和定义 EditItemTemplate 元素。这使得您的应用程序可编辑变得更加容易。

在 formView 中,您可能需要将 DefaultMode 更改为 Edit。也在你的代码后面尝试:

protected void fvReport_ModeChanging(Object sender, FormViewModeEventArgs e)
{

}

protected void lbEdit_Click(object sender, EventArgs e)
{
    LinkButton lbEdit = (LinkButton)fvReport.FindControl("lbEdit");

    if (sender == lbEdit)
    {
        fvReport.DataBind();
        fvReport.ChangeMode(FormViewMode.Edit);
    }
}

Another option which I commonly use to go into edit mode from a formView is to add and define the EditItemTemplate element. This makes it a lot easier to make your application editable.

Within your formView you may need to change your DefaultMode to Edit. Also in your code behind try:

protected void fvReport_ModeChanging(Object sender, FormViewModeEventArgs e)
{

}

protected void lbEdit_Click(object sender, EventArgs e)
{
    LinkButton lbEdit = (LinkButton)fvReport.FindControl("lbEdit");

    if (sender == lbEdit)
    {
        fvReport.DataBind();
        fvReport.ChangeMode(FormViewMode.Edit);
    }
}
始终不够爱げ你 2024-11-09 17:21:26

您的 FormView 未切换可能还有其他原因。这通常是由于 HTML 格式错误造成的。您的设计器有时会通过显示类似以下内容来告诉您格式错误的部分...

在此处输入图像描述

在某些情况下,当您没有收到此明显消息时,FormView 未切换通常是到一些不太明显的事情,比如同样糟糕的 AssociatedControlId 属性。我建议您查看标签、验证器以及任何控件必须与另一个控件关联的内容。像这样小的东西...

<asp:Label runat="server"
    ID="labelAccessGrantedBy"
    Text="Access Granted By"
    AssociatedControlID="textAccessGranted" />
<asp:TextBox runat="server"
    ID="textAccessGrantedBy"
    CssClass="wmioSmall wFull"
    Text='<%# Bind("access_granted_by") %>' />

注意到上面故意使用了 textAccessGranted,而不是实际的 textAccessGrantedBy TextBox 吗?这就是我过去的命令处理失败的地方。

There could be other reasons why your FormView isn't switching over. It's usually down to badly formatted HTML. Your designer sometimes tells you of malformed sections by displaying something like this...

enter image description here

On those occasions when you don't get this obvious message, FormView not switching over is usually down to something a little less obvious, such as bad AssociatedControlId attributes. I would recommend looking at you labels, validators and anything where a control has to be associated to another. Something as small as this...

<asp:Label runat="server"
    ID="labelAccessGrantedBy"
    Text="Access Granted By"
    AssociatedControlID="textAccessGranted" />
<asp:TextBox runat="server"
    ID="textAccessGrantedBy"
    CssClass="wmioSmall wFull"
    Text='<%# Bind("access_granted_by") %>' />

Notice the deliberate use of textAccessGranted above, rather than the actual textAccessGrantedBy TextBox? That's where the command handling has failed for me in the past.

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