我有this发布中描述的嵌套ListView(主/明细)问卷:内部列表项必须使用三个参数调用服务器方法。感谢你们中的一些人,我有正确生成JavaScript的代码;但是,该代码实际上并未触发。这是内部列表视图的声明:
<asp:ListView ID="lstAnswers" runat="server" DataKeyNames="QuestionID" OnItemDataBound="lstAnswers_ItemDataBound">
<LayoutTemplate>
<tr id="ItemPlaceholder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td style="vertical-align: top; font-weight: bold;">
<asp:Label ID="lblPAQID" runat="server" Text='<%#Eval("ThisPAQID") %>' Visible="false" />
<asp:Label ID="lblPAQVersion" runat="server" Text='<%#Eval("PAQVersion") %>' Visible="false" />
<asp:Label ID="lblQuestionID" runat="server" Text='<%#Eval("QuestionID")%>' />
</td>
<td style="vertical-align: top;">
<asp:Label ID="lblQuestionText" runat="server" Text='<%#Eval("Question")%>' />
</td>
<td style="vertical-align: top;">
<asp:TextBox ID="txtAnswer" runat="server" Class="PAQAnswer" Text='<%#Eval("Answer")%>' />
<script type="text/javascript"> **THIS SCRIPT IS NOT FIRING**
var textBox = document.getElementById("txtAnswer");
if (document.attachEvent) {
document.attachEvent("onblur", textBox, function() {
SaveAnswer('<%#Eval("ThisPAQID")%>', '<%#Eval("QuestionID")%>', window.event.target.value);
});
} else {
document.addEventListener("blur", textBox, function() {
SaveAnswer('<%#Eval("ThisPAQID")%>', '<%#Eval("QuestionID")%>', this.value);
}, false);
}
</script>
</td>
</tr>
</ItemTemplate>
</asp:ListView>
生成的代码如下所示:
<input name="lstQuestions$ctrl0$lstAnswers$ctrl0$txtAnswer" type="text" value="2.0" maxlength="3" id="lstQuestions_ctrl0_lstAnswers_ctrl0_txtAnswer" Class="PAQAnswer" style="width:30px;" />
<script type="text/javascript">
var textBox = document.getElementById("txtAnswer");
if (document.attachEvent) {
document.attachEvent("onblur", textBox, function() {
SaveAnswer('13242', '1', window.event.target.value);
});
} else {
document.addEventListener("blur", textBox, function() {
SaveAnswer('13242', '1', this.value);
}, false);
}
</script>
SaveAnswer调用中的嵌入值是正确的;但是,我不知道如何将输入语句绑定到JavaScript。可能非常简单,但我还没看到......但是。
答案 0 :(得分:2)
这一行看起来像问题:
var textBox = document.getElementById("txtAnswer");
txtAnswer
不是控件的实际ID,因此您的脚本找不到它。输入的ID为lstQuestions_ctrl0_lstAnswers_ctrl0_txtAnswer
你应该尝试类似的东西:
var textBox = document.getElementById("<%= txtAnswer.ClientID %>");
将转换为:
var textBox = document.getElementById("lstQuestions_ctrl0_lstAnswers_ctrl0_txtAnswer");
编辑:为什么不这样做?
<asp:TextBox ID="txtAnswer" runat="server" Class="PAQAnswer" Text='<%#Eval("Answer")%>' onBlur="SaveAnswer('<%#Eval("ThisPAQID")%>', '<%#Eval("QuestionID")%>', this.value);" />
答案 1 :(得分:0)
尝试附加如下事件:
var textBox = document.getElementById("txtAnswer");
if (textBox.attachEvent) {
textBox.attachEvent("onblur", function() {
SaveAnswer('13242', '1', window.event.target.value);
});
} else {
textBox.addEventListener("blur", function() {
SaveAnswer('13242', '1', this.value);
}, false);
}
PS:我认为Victor也是对的,txtAnswer
不是您节点的实际ID。