本文介绍了在页面加载事件中单击一个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在页面加载事件期间执行onclick操作
请告诉我
I want to perform onclick operation during page load event
please tell me
推荐答案
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ZTest2.aspx.cs" Inherits="ZTest2" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script language="javascript" type="text/javascript">
function getServerTime() {
var btnServer = document.getElementById('<%=btnGetTime.ClientID %>');
btnServer.click();
}
function clientClick() {
var btnClient = document.getElementById('btnClient');
btnClient.click();
}
function getClientTime() {
var clientText = document.getElementById('<%=txtClientTime.ClientID %>');
var dt = new Date();
clientText.value = dt.getDate() + "-" + dt.getMonth() + "-" + dt.getFullYear() + " " + dt.getHours() + ":" + dt.getMinutes() + ":" + dt.getSeconds();
}
function pageLoadFunction() {
clientClick();
//getServerTime();
}
</script>
</head>
<body onload="pageLoadFunction();">
<form id="form1" runat="server">
<table cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center">
<table cellpadding="2" cellspacing="2">
<tr>
<td>
Getting server time using Javascript
</td>
<td>
<asp:TextBox runat="server" ID="txtServerTime"></asp:TextBox>
</td>
<td>
<asp:Button runat="server" ID="btnGetTime" Text="Get Server Time" OnClick="btnGetTime_Click" />
</td>
</tr>
<tr>
<td>
Getting client time using Javascript
</td>
<td>
<asp:TextBox runat="server" ID="txtClientTime"></asp:TextBox>
</td>
<td>
<input type="button" id="btnClient" value="Get Client Time" onclick="getClientTime();" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>
这是服务器端代码:
Here is server side code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ZTest2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnGetTime_Click(object sender, EventArgs e)
{
txtServerTime.Text = DateTime.Now.ToString("dd-MMM-yy hh:mm:ss");
}
}
如有任何疑问,请让我知道.
如果对您有帮助,请提供投票.
谢谢,
Imdadhusen
Please do let me know, if you have any doubt.
Please provide Vote if this would be helpful to you.
Thanks,
Imdadhusen
<form id="form1" runat="server">
<div>
<asp:Button ID="btn" Text="Submit" runat="server" onclick="btn_Click" />
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
btn_Click(sender, e);
}
protected void btn_Click(object sender, EventArgs e)
{
Response.Write("button click event executed");
}
这篇关于在页面加载事件中单击一个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!