我有一个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>
这样可以正常工作,btnAdd命中ItemInserting函数,该函数声明如下:
Protected Sub lvContacts_ItemInserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewInsertEventArgs) Handles lvContacts.ItemInserting
...
End Sub
在ItemCreated函数中,我添加了一个“初始化”TextBox的代码,方法是添加一个带有JavaScript函数的属性,该函数有效地将虚拟文本添加到Control中,当用户点击它时会消失。但是,如果我保留此代码,则不再访问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不再有效?
答案 0 :(得分:1)
在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
答案 1 :(得分:0)
Ali Shahrokhi将此推向了正确的方向,当之无愧地得到了赏金。为解决此问题,我们从InitialiseTextbox
部分删除了ItemCreated
功能,并添加了以下内容:
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
的文本框。