我有一个带有2个ItemTemplates的gridview- 一个checkBox,一个TextBox 我必须使用Javascript验证Validate4 如果选中了复选框,则复选框中不会写入数量!! 我必须通知客户我该怎么办? 复选框和文本框与gridview在同一行中,如所理解的那样。 Plz给我剧本 提前谢谢。
答案 0 :(得分:1)
如果您使用的是jQuery,则可以轻松完成。 创建一个函数,它将“this”作为参数,并在复选框“Click”事件中将“this”传递给函数。然后使用jQuery的.prev()方法,你可以获得网格的“tr”行
var trContainer = $(this).prev("tr")
一旦你做了(使用jQuery find)
//如果行中只有一个文本框
var txtBox = $(trContainer).find("input");
//如果您有多个文本框
//在此文本框中添加一些虚拟css类,例如txtValidateClass
var txtBox = $(trContainer).find(".txtValidateClass");
现在有了这个文本框,只需检查
if($(txtBox).val().trim() == ""){
//Show error
}else{
//Continue
}
希望这会对你有所帮助。
答案 1 :(得分:0)
问题是陈旧的,但以防万一其他人需要一个只适用于JavaScript的解决方案就是这样。
Page_Load
方法中)在Page_Load
我们需要添加一个小黑客:
foreach(GridViewRow row in YourGridViewControlID.Rows)
{
if (row.RowType == DataControlRowType.DataRow )
{
((CheckBox) row.FindControl("YourCheckBoxID")).Attributes.Add("onchange", "javascript:ShowErrorMessage(" + (row.RowIndex ) + ");");
}
}
这样,我们就会在OnChange
CheckBox
的每个GridView
HTML
事件中添加JavaScript函数调用。什么是特殊的,我们无法通过Row Index
实现的是我们在JavaScript函数中传递每个Checkbox
,这是我们以后需要的。
确保Textbox
控件和GridView
控制,但更重要您的ClientIDMode="Static"
控件具有静态ID,使用<asp:CheckBox ID="YourCheckBoxID" runat="server" ClientIDMode="Static"/>
<asp:TextBox ID="YourTextBoxID" TextMode="SingleLine" runat="server" ClientIDMode="Static" />
,如图所示波纹管:
GridView
对于<asp:GridView ID="YourGridViewControlID" ...... ClientIDMode="Static" runat="server">
控件:
function ShowErrorMessage(Row) {
// Get current "active" row of the GridView.
var GridView = document.getElementById('YourGridViewControlID');
var currentGridViewRow = GridView.rows[Row + 1];
// Get the two controls of our interest.
var rowTextBox = currentGridViewRow.cells[2].getElementsByTagName("input")[0];
var rowCheckbox = currentGridViewRow.cells[0].getElementsByTagName("input")[0];
// If the clicked checkbox is unchecked.
if( rowCheckbox.checked && rowTextBox.value === '') {
// Empty textbox therefore show error message.
return;
}
// Optionally hide the error message here.
}
然后在您的JavaScript文件/代码中:
var currentGridViewRow = GridView.rows[Row + 1];
[Row + 1]
var rowTextBox = currentGridViewRow.cells[2].getElementsByTagName("input")[0];
var rowCheckbox = currentGridViewRow.cells[0].getElementsByTagName("input")[0];
对于完成这项工作很重要,不应该改变。以下几行:
.cells[2]
.cells[0]
和[]
可能与您有所不同,因此您必须在GridView
中选择正确的数字。
通常,这将是从0
开始计算的CheckBox
的列号。
因此,如果您的.cells[0]
位于GridView的第一列,那么您需要TextBox
。
如果您的GridView
位于.cells[1]
的第二列,那么您需要TextBox
(在我的情况下,GridView
位于.cells[2]
的第三列因此,我使用foreach(GridViewRow row in YourGridViewControlID.Rows)
{
if (row.RowType == DataControlRowType.DataRow )
{
((TextBox) row.FindControl("YourTextBoxID")).Attributes.Add("onkeyup", "javascript:HideErrorMessage(" + (row.RowIndex ) + ");");
((TextBox) row.FindControl("YourTextBoxID")).Attributes.Add("onblur", "javascript:HideErrorMessage(" + (row.RowIndex ) + ");");
}
}
)。
以上是一个代码,可以在问题所有者描述问题时使用。
但我认为在这种情况下,在对文本框进行更改时隐藏错误消息也是一个好主意。这是当用户选中复选框并且文本框为空时,收到错误消息,然后在文本框中键入内容,我们应该隐藏错误消息。
此处简要介绍了上述注释的添加内容(为简单起见,我已删除了其他代码,但如果要保留整个功能,则应保留两者:)
这是C#修改:
onblur
从这里可以看出,他已经添加了onkeyup
事件,因为这个事件与function HideErrorMessage(Row) {
// Get current "active" row of the GridView.
var GridView = document.getElementById('YourGridViewControlID');
var currentGridViewRow = GridView.rows[Row + 1];
// Get the two controls of our interest.
var rowTextBox = currentGridViewRow.cells[2].getElementsByTagName("input")[0];
var rowCheckbox = currentGridViewRow.cells[0].getElementsByTagName("input")[0];
// If the clicked checkbox is unchecked.
if( rowCheckbox.checked && rowTextBox.value !== '') {
// Hide the error message here.
}
}
的组合我们确保代码将在所有情况下执行。
以下是JavaScript修改:
onMessage()