【问题标题】:ListView not firing OnItemCommand (nor ItemInserting) after preventing postback防止回发后 ListView 未触发 OnItemCommand(也不是 ItemInserting)
【发布时间】:2010-06-08 17:44:44
【问题描述】:

我在 FormView 中有一个 ListView,由于某种奇怪的原因,它既不会触发 ItemInsert 也不会触发 ItemCommand 事件。 我正在使用通用列表填充 ListView。我将列表绑定到 OnPreRenderComplete 上的 ListView。

    <asp:ListView runat="server" ID="lvReferences" DataKeyNames="idReference" OnItemInserting="ContractReferences_Inserting" OnItemDeleting="ContractReferences_Deleting" InsertItemPosition="LastItem" OnItemCommand="ContractReferences_Command" OnItemCreated="ContractReferences_ItemDataBound">                                    
    <LayoutTemplate>
        <ul>       
                <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
        </ul>                                   
    </LayoutTemplate>
    <ItemTemplate>
        <li class="obsItem">
            <a href="#"><asp:TextBox ID="valRef" runat="server" Width="5px" Enabled="false" Text='<%#Bind("idProcessRecordRef") %>' /></a>                                    
            <asp:TextBox id="txtRef" runat="server" Text='<%#Bind("description") %>' />
            <asp:ImageButton ID="btDelete" runat="server" CommandName="Delete" ImageUrl="~/_layouts/web.commons/Images/eliminar.png" />
        </li>
    </ItemTemplate> 
    <InsertItemTemplate>
        <li class="obsItem">
            <a href="#"><asp:TextBox ID="valRef" runat="server" Width="5px" Enabled="false" /></a>                                    
            <asp:TextBox id="txtRef" runat="server" />
            <asp:ImageButton ID="btDetail" CausesValidation="false" OnClientClick="javascript:openPopup();return false;" runat="server" ImageUrl="~/_layouts/web.commons/Images/novo.png" />
            <asp:ImageButton ID="btSaveDs" runat="server" CommmandName="Insert" CausesValidation="false" ImageUrl="~/_layouts/web.commons/Images/gravarObs.png" />
        </li>
    </InsertItemTemplate>
</asp:ListView>   

我的 ItemDataBound 方法是:

    protected void ContractReferences_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox valRef = e.Item.FindControl("valRef") as TextBox;
            TextBox txtRef = e.Item.FindControl("txtRef") as TextBox;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "function openPopup(){ window.open('ContractPicker.aspx?c1=" + valRef.ClientID + "&c2=" + txtRef.ClientID + "');}", true);
        }
    }

因此,基本上,我在 InsertItemTemplate 中放置了一个按钮,用于打开 LOV 并填充我的 valRef 和 txtRef 字段。我必须输入“return false”才能使父页面不回发(我认为问题出在此处……)。 然后,当我单击带有 CommandName="Insert" 的 ImageButton 时,它不会触发 ItemCommand 事件,而是再次进入 ItemDataBound 处理程序。

那么,有什么想法吗?

谢谢!

【问题讨论】:

  • 顺便说一句,构建复杂的字符串建议尝试String.Format()
  • 是的,我知道,我在使用 String.Format 时遇到了一个严重的错误,所以我在这方面浪费了很多时间,我用连接构建了字符串,直到我解决了这个错误。

标签: c# asp.net listview postback


【解决方案1】:

好吧,搜索了几天后,我发现这是一个已知的错误。 http://connect.microsoft.com/VisualStudio/feedback/details/328680/problem-accessing-controls-clientid-on-asp-net-listviews-itemcreated

因此,它与弹出窗口无关,而是与 ListView 和 ItemDataBound 本身有关。我将 ItemDataBound 逻辑放在 OnLoad 上(使用递归 FindControl),它起作用了。 因此,在 ItemDataBound 中,我的控件的 ClientID 是“错误的”。 真是个笨蛋……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-13
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多