ListView没有't在向TextBox添加属性后激发ItemInserting
ListView doesn't fire ItemInserting after adding an attribute to a TextBox
我有一个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
的文本框。
相关文章:
- 选中单选框时将属性添加到输入字段
- 将css属性添加到对象的集合中
- 用javascript向数组(?)中的特定属性添加值
- AngularJs,在没有隔离的情况下将指令属性添加到作用域
- 如何将enctype属性添加到FormData()对象中
- 如何将属性添加到自定义角度指令
- 如何将额外的属性添加到bootstrapTable中
- 在 Javascript 生成的列表中,如何将自定义属性添加到 标记
- 将动态属性添加到对象末尾
- 向 JSON 对象属性添加引号
- 将地理 JSON 要素属性添加到地图框弹出窗口
- 是否有脚本/代码可以一次将链接属性添加到多个链接
- 将自定义属性添加到 vuejs 组件
- 将属性添加到带有href链接但没有#的元素
- 将属性添加到数组中的所有对象
- Jquery将data-rel属性添加到所有输入字段,并移除name属性
- 将属性添加到窗元素时是否引发事件
- 将onsubmit属性添加到表单并提交
- 将属性添加到数组中的每个对象
- 如何使用jQuery向属性添加值