gpt4 book ai didi

javascript - 将属性添加到 TextBox 后,ListView 不会触发 ItemInserting

转载 作者:行者123 更新时间:2023-12-03 12:43:59 24 4
gpt4 key购买 nike

我有一个 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 函数中,我通过添加带有 JavaScript 函数的属性来“初始化”文本框,该代码可以有效地将虚拟文本添加到控件中,当用户单击该控件时该文本就会消失。但是,如果我保留此代码,则将不再达到 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 的方法可以正常工作,并且也可以正常工作。任何想法为什么当我这样做时 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

关于javascript - 将属性添加到 TextBox 后,ListView 不会触发 ItemInserting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23408737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com