在gridview中获取文本框ID的Javascript不起作用 [英] Javascript to get the id of textbox inside gridview doesn't work

查看:86
本文介绍了在gridview中获取文本框ID的Javascript不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用ASP:Textbox的网络表单,并希望对其执行空检查。我想在文本框上使用javascript进行验证。



下面是我单击ImageButton时调用的Jscript。问题是,如果我将该文本框放在gridview之外,则验证效果很好。但是在gridview内部不起作用。我得到的错误是:当前上下文中不存在名称TxtInputDate。请帮帮我。





代码:

I have web forms which uses ASP:Textbox and would like to perform null check on it. I would like to use javascript on the textbox for validation.

Below is my Jscript which is called when ImageButton is clicked. The problem is that if I place that textbox outside the gridview, the validation works great. But doesn't works inside the gridview. I get the error as : The name 'TxtInputDate' doesn't exist in the current context. Please help me out.


Code:

function DateValidate() {
    if ((document.getElementById("<%=TxtInputDate.ClientID%>").value).length == 0) {
        alert("Invalid Date.!");
        return false;
    }
    return true;
}







<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound" 

	OnPageIndexChanging="GridView1_PageIndexChanging" 

	OnRowCancelingEdit="GridView1_RowCancelingEdit"  OnRowDeleting="GridView1_RowDeleting"

	OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"

	AutoGenerateColumns="False" ShowFooter="true" OnRowCommand="GridView1_RowCommand" >

   <Columns>
		<asp:templatefield headertext="Holiday List" ItemStyle-Width="90px">
		   <itemtemplate>
			   <asp:Label ID="lblDate" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Date") %>'>
			   </asp:Label>
		   </itemtemplate>

		   <edititemtemplate>
			  <asp:textbox id="HolidayListTextBox"

				 text='<%#Eval("Date")%>'

				 width="90"

				 Style="text-align:center;"

				 runat="server"/>
		   </edititemtemplate>

			<FooterTemplate>
				<asp:TextBox ID="TxtInputDate" runat="server" Width="90px" Style="text-align:center; cursor:pointer" onpaste="return false" onKeyPress="javascript:return false;" ></asp:TextBox>
				<ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TxtInputDate"

						Format="dd MMM yyyy" PopupButtonID="TxtInputDate"/>
			</FooterTemplate>
			<ItemStyle Width="90px"></ItemStyle>
		</asp:templatefield>

		<asp:TemplateField HeaderText="Action" ItemStyle-Width="60px">
			<ItemTemplate>
			   <asp:ImageButton ID="imgbtnEdit" runat="server" CommandName="Edit" ImageUrl="~/Images/icon-edit.png" Height="25px" Width="25px"/>
			   <asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Delete.png"  Height="25px" Width="25px"/>
			</ItemTemplate>
			<EditItemTemplate>
			   <asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"  Height="25px" Width="25px"/>
			   <asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"  Height="25px" Width="25px"/>
			</EditItemTemplate>

			<FooterTemplate>
				<asp:ImageButton ID="lbtnAdd" runat="server" CommandName="ADD" OnClientClick="javascript:return TxtboxValidation();" ImageUrl="~/Images/icon-add.png" Height="25px" Width="25px"></asp:ImageButton>
			</FooterTemplate>
			<ItemStyle Width="60px"></ItemStyle>
		</asp:TemplateField>
	</Columns>
</asp:GridView>

推荐答案

查看从客户端看到的HTML。只需使用浏览器的View =>页面源(或类似的东西)。在 document.getElementById id id c>匹配。如果匹配,请使用JavaScript调试器查看接下来会发生什么。如果它们不匹配且您看不到可以执行的操作,请阅读ASP.NET中ID的生成:

https://msdn.microsoft.com/en-us/library/system.web.ui。 control.clientid%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v = vs.110).aspx [ ^ ]。



-SA
Look at the HTML as it seen from the client side. Simply use your browser's View => Page Source (or something like that). Check up that HTML element's id and the id in document.getElementById match. If they match, use the JavaScript debugger to see what happens next. If they don't match and you don't see what you can do, read on the generation of IDs in ASP.NET:
https://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode(v=vs.110).aspx[^].

—SA


这篇关于在gridview中获取文本框ID的Javascript不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆