ListView没有't在向TextBox添加属性后激发ItemInserting

ListView doesn't fire ItemInserting after adding an attribute to a TextBox

本文关键字:属性 添加 ItemInserting TextBox 在向 没有 ListView      更新时间:2023-09-26

我有一个ListView可以在数据库中输入新的联系人,在我将自定义属性添加到InsertItemTemplate中的文本框中以提供一些JavaScript功能之前,它运行良好。

以下是ListView:的一个片段

    <asp:ListView ID="lvContacts" runat="server" InsertItemPosition="LastItem">
        <LayoutTemplate>
            <div id="itemPlaceholder" runat="server">
            </div>
        </LayoutTemplate>
        <ItemTemplate>
            ...
        </ItemTemplate>
        <InsertItemTemplate>
            <div class="borderedBox">
                <h3>
                    ADD OTHER CONTACT</h3>
                <div>
                    <div class="leftFields">
                        <asp:Label ID="astContactType" runat="server" CssClass="fieldLabel">Contact Type *</asp:Label><br />
                        <asp:TextBox ID="txtContactType" runat="server"></asp:TextBox><br />
                    </div>
                    <div class="rightFields">
                        <asp:Button ID="btnAdd" runat="server" Text="ADD NEW" Style="width: 150px; margin-top: 27px; margin-left: 0px;" CommandName="Insert" />
                    </div>
                    <br style="clear: both;" />
                </div>
            </div>
            ...
        </InsertItemTemplate>
    </asp:ListView>

这可以很好地工作,并且btAddd命中ItemInserting函数,该函数声明如下:

Protected Sub lvContacts_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles lvContacts.ItemInserting
    ...
End Sub

在ItemCreated函数中,我添加了一段代码,通过添加一个带有JavaScript函数的属性来"初始化"TextBox,该函数可以有效地将伪文本添加到控件中,当用户单击控件时,控件就会显示。但是,如果我保留此代码,则无法再访问ItemInserting函数。以下是ItemCreated函数:

Protected Sub lvContacts_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles lvContacts.ItemCreated
    Dim txtContactType As TextBox = e.Item.FindControl("txtContactType")
    ControlHelper.InitialiseTextbox(txtContactType, "Contact Type")
End Sub
(This next part is in a separate file in the App_Code folder)
Public Class ControlHelper
    Public Shared Sub InitialiseTextbox(ByVal Text As TextBox, ByVal DefaultValue As String)
        Text.Text = DefaultValue
        Text.Attributes.Add("onfocus", "textFocus('" & Text.ClientID & "', '" & DefaultValue & "')")
        Text.Attributes.Add("onblur", "textBlur('" & Text.ClientID & "', '" & DefaultValue & "')")
    End Sub
End Class

这种初始化TextBox的方法通过向TextBox添加属性来正确工作,TextBox也可以正常工作。有什么想法为什么当我这样做的时候ItemInserting不再工作了吗?

在ListView控件上再添加两个事件属性,如下所示:

 <asp:ListView ID="lvContacts" runat="server" InsertItemPosition="LastItem" 
          OnItemInserting="lvContacts_ItemInserting"
          OnItemEditing="lvContacts_ItemEditing" 
          OnPreRender="lvContacts_PreRender" >

然后像这样更改您的代码:

首先在类作用域中设置一个整数变量,然后在ItemEditing事件中为变量设置索引值,并使用pre_render事件在控件上设置属性:

Private itemIndex As Integer = -1
Protected Sub lvContacts_ItemEditing(sender As Object, e As ListViewEditEventArgs)
     itemIndex = e.NewEditIndex
End Sub
Protected Sub lvContacts_PreRender(sender As Object, e As EventArgs)
    Dim txtContactType As TextBox
    If itemIndex <> -1 Then
        txtContactType = DirectCast(ListView1.Items(itemIndex).FindControl("txtContactType"), TextBox)
        ControlHelper.InitialiseTextbox(txtContactType, "Contact Type")
    End If
End Sub

Ali Shahrokhi将其推向了正确的方向,理所当然地获得了赏金。为了解决这个问题,我们从ItemCreated部分删除了InitialiseTextbox函数,而是添加了以下内容:

Protected Sub lvContacts_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvContacts.PreRender
    Dim txtContactType As TextBox
    For Each item As ListViewItem In lvContacts.Items
        txtContactType = item.FindControl("txtContactType")
        ControlHelper.InitialiseTextbox(txtContactType, "Contact Type")
    Next
    txtContactType = lvContacts.InsertItem.FindControl("txtContactType")
    ControlHelper.InitialiseTextbox(txtContactType, "Contact Type")
End Sub

这将遍历ListView中的每个项目并初始化它们,然后初始化InsertItem的文本框。