如何通过单独的javascript文件调用网页中定义的服务器控件 [英] how to call server control defined in web page through separate javascript file
本文介绍了如何通过单独的javascript文件调用网页中定义的服务器控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何通过单独的javascript文件调用网页中的控件定义
喜欢
how to call control define in web page through separate javascript file
like
<asp:TextBox id="txt" runat="server" />
<asp:Button id="btn" runat="server" onClientClick="abc()"/>
<script type='text/javascript' src='abc.js'/>
javascript文件abc.js
javascript file abc.js
function abc()
{
alert(document.getElementByID('<%=txt.ClientID%>').value);
}
推荐答案
您需要将控件的ID作为javascript函数的参数进行传递.
作为参考,请参见下面的代码.
您的Javascript文件abc.js包含以下内容
You need to pass the ID of the control as a parameter of your javascript function.
For reference, see the code below.
Your Javascript file abc.js contains following
function abc(txtID) {
alert(document.getElementById(txtID).value);
}
HTML页面包含,
HTML page contains,
<script src="abc.js" type="text/javascript"></script>
<asp:textbox id="txtInput" runat="server" ></asp:textbox>
<asp:button id="btn2" runat="server" text="Click Me" />
然后在服务器端页面加载事件
Then on your server side page load event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btn2.Attributes.Add("onclick", "abc('" + txtInput.ClientID + "')");
}
}
就这样,您完成了.
如果此解决方案适合您,请标记为答案并投票.
That''s it, You are done.
Do Mark as Answer and Vote, if this solution works for you.
这篇关于如何通过单独的javascript文件调用网页中定义的服务器控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文