js获取.aspx页面里面的服务器控件和.ascx中的服务器控件值
2020-11-27
来源:伴沃教育
A.aspx页面
代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="A.aspx.cs" Inherits="OrderManage_A" %>
<%@ Register Src="../UserControl/CtrlCalendar.ascx" TagName="CtrlCalendar" TagPrefix="uc1" %>
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title></title>
<script language="javascript" type="text/javascript">
function GetValue()
{
var txpro=document.getElementById('<%=txtProvider_rename.ClientID %>');
//用js获取服务器控件的值时首先要得到服务器控件的ClientID
var txdat=document.getElementById('<%=txtCheckIn.ClientID %>'+"_txtDateVal");
//用js获取.ascx控件中服务器控件值时首先要得到服务器控件的ClientID再加上.ascx页面里面的服务器空间ID并用"_"连接
txpro.value=txdat.value;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<tr>
<td align="right" bgcolor="#dee5fa">入住日期</td>
<td align="left" bgcolor="#dee5fa"><uc1:CtrlCalendar ID="txtCheckIn" runat="server" /></td>
</tr>
<tr>
<td align="right" bgcolor="#dee5fa" onclick="GetValue()"><asp:TextBox ID="txtProvider_rename" runat="server" Width="312px"></asp:TextBox></td>
</tr>
</div>
</form>
</body>
</html>
CtrlCalendar.ascx
代码如下:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="CtrlCalendar.ascx.cs" Inherits="CtrlCalendar" %>
<table id="Table1" style="HEIGHT: 24px" cellspacing="0" cellpadding="0" width="100" border="0">
<tr>
<td style="WIDTH: 81px"><font face="宋体">
<asp:textbox id="txtDateVal" Width="84px" runat="server" BorderStyle="Groove"></asp:textbox></font></td>
<td><div id="Div_imgCanlendar"><font face="宋体"><img id="imgCanlendar" style="CURSOR: hand" alt="" src="../images/calendar.gif" runat="server" /></font></div></td>
</tr>
</table>
显示全文