我在使用网格视图中的JavaScript在自动完成模式下捕获所选数据时遇到麻烦。实际上,前两个字段是成功的,但是,当我添加第三个字段时,该按钮不会触发捕获数据的事件。我无法查明我做错了什么,因为没有错误发给我。好像按钮已被禁用。
这是完整的代码段:
<asp:GridView ID="grdSafeZone" runat="server" CssClass="table"
GridLines="Horizontal" AutoGenerateColumns="false" UseAccessibleHeader="true"
Width="1500" ShowFooter="true">
<Columns>
<asp:TemplateField HeaderText="Branch">
<ItemTemplate>
<%# Eval("Branch") %>
</ItemTemplate>
<FooterTemplate>
<div class="form-group">
<asp:TextBox ID="txtBranch" runat="server" CssClass="form-control" placeholder="Enter Branch"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
ServiceMethod="GetBranch"
MinimumPrefixLength="1"
CompletionInterval="10"
EnableCaching="false"
CompletionSetCount="1"
TargetControlID="txtBranch"
FirstRowSelected="false"
OnClientItemSelected="BranchSelected">
</ajaxToolkit:AutoCompleteExtender>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee's Name">
<ItemTemplate>
<%# Eval("EmployeeName") %>
</ItemTemplate>
<FooterTemplate>
<div class="form-group">
<asp:TextBox ID="txtEmployeeName" runat="server" CssClass="form-control" placeholder="Enter Employee Name"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server"
ServiceMethod="GetEmployee"
MinimumPrefixLength="2"
CompletionInterval="10"
EnableCaching="false"
CompletionSetCount="1"
TargetControlID="txtEmployeeName"
FirstRowSelected="false"
OnClientItemSelected="EmployeeSelected">
</ajaxToolkit:AutoCompleteExtender>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buddy 1">
<ItemTemplate>
<%# Eval("Buddy1") %>
</ItemTemplate>
<FooterTemplate>
<div class="form-group">
<asp:TextBox ID="txtBuddyOne" runat="server" CssClass="form-control" placeholder="Enter Buddy 1"></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender3" runat="server"
ServiceMethod="GetBuddyOne"
MinimumPrefixLength="2"
CompletionInterval="10"
EnableCaching="false"
CompletionSetCount="1"
TargetControlID="txtBuddyOne"
FirstRowSelected="false">
</ajaxToolkit:AutoCompleteExtender>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buddy 2">
<ItemTemplate>
<%# Eval("Buddy2") %>
</ItemTemplate>
<FooterTemplate>
<div class="form-group">
<asp:TextBox ID="txtBuddyTwo" runat="server" CssClass="form-control" placeholder="Enter Buddy 2" value=""></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender4" runat="server"
ServiceMethod="GetEmployees"
MinimumPrefixLength="2"
CompletionInterval="10"
EnableCaching="false"
CompletionSetCount="1"
TargetControlID="txtBuddyTwo"
FirstRowSelected="false">
</ajaxToolkit:AutoCompleteExtender>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buddy 3">
<ItemTemplate>
<%# Eval("Buddy3") %>
</ItemTemplate>
<FooterTemplate>
<div class="form-group">
<asp:TextBox ID="txtBuddyThree" runat="server" CssClass="form-control" placeholder="Enter Buddy 3" value=""></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender5" runat="server"
ServiceMethod="GetEmployees"
MinimumPrefixLength="2"
CompletionInterval="10"
EnableCaching="false"
CompletionSetCount="1"
TargetControlID="txtBuddyThree"
FirstRowSelected="false">
</ajaxToolkit:AutoCompleteExtender>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Buddy 4">
<ItemTemplate>
<%# Eval("Buddy4") %>
</ItemTemplate>
<FooterTemplate>
<div class="form-group">
<asp:TextBox ID="txtBuddyFour" runat="server" CssClass="form-control" placeholder="Enter Buddy 4" value=""></asp:TextBox>
<ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender6" runat="server"
ServiceMethod="GetEmployees"
MinimumPrefixLength="2"
CompletionInterval="10"
EnableCaching="false"
CompletionSetCount="1"
TargetControlID="txtBuddyFour"
FirstRowSelected="false">
</ajaxToolkit:AutoCompleteExtender>
</div>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate></ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnSave" runat="server" CssClass="btn btn-secondary" Text="Save New Entry" CommandName="Footer" OnClick="btnSave_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<table>
<tr>
<th>Employee Name</th>
<th>Buddy 1</th>
<th>Buddy 2</th>
<th>Buddy 3</th>
<th>Buddy 4</th>
<th></th>
</tr>
<tr>
<td><asp:TextBox ID="txtBranch" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="txtEmployeeName" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="txtBuddyOne" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="txtBuddyTwo" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="txtBuddyThree" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="txtBuddyFour" runat="server"></asp:TextBox></td>
<td><asp:Button ID="btnSave" runat="server" Text="Save New Entry" CssClass="btn btn-secondary" CommandName="EmptyDataTemplate" OnClick="btnSave_Click" /></td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
<!-- *********** Branch ID *********** -->
<script type = "text/javascript">
function BranchSelected(source, eventArgs) {
var hdeBranchID = "<%= hdeBranchID.ClientID %>";
document.getElementById(hdeBranchID).value = eventArgs.get_value();
__doPostBack(hdeBranchID, "");
}
</script>
<asp:HiddenField ID="hdeBranchID" runat="server" />
<!-- *********** Employee ID *********** -->
<script type = "text/javascript">
function EmployeeSelected(source, eventArgs) {
var hdeEmployeeID = "<%= hdeEmployeeID.ClientID %>";
document.getElementById(hdeEmployeeID).value = eventArgs.get_value();
__doPostBack(hdeEmployeeID, "");
}
</script>
<asp:HiddenField ID="hdeEmployeeID" runat="server" />
<!-- *********** Buddy One ID *********** -->
<script type = "text/javascript">
function BuddyOneSelected(source, eventArgs) {
var hdeBuddyOneID = "<%= hdeBuddyOneID.ClientID %>";
document.getElementById(hdeBuddyOneID).value = eventArgs.get_value();
__doPostBack(hdeBuddyOneID, "");
}
</script>
<asp:HiddenField ID="hdeBuddyOneID" runat="server" />
VB中的代码后面:
Protected Sub btnSave_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim grdRow As GridViewRow = CType(btn.Parent.Parent, GridViewRow)
' gets the client id of each data selected from the auto complete list
Dim branchId As String = hdeBranchID.Value
Dim employeeId As String = hdeEmployeeID.Value
Dim buddyOneId As String = hdeBuddyOneID.Value
End Sub
我想做的是从自动完成列表中选择一个项目时,保存按钮应该使用JS的ClientID捕获数据-前两个工作正常,当我添加第三个(好友一个)时,一切停止工作。
如果您知道问题所在,将不胜感激,否则,我对采用ClientID而非文字数据捕获所有数据的方法持开放态度。
谢谢!