日期:2014-05-17  浏览次数:20424 次

求大神帮忙onclientclick调onclick问题
在网页前台有两个按钮,在其中一个按钮的onclientclick中通过document调用另一个按钮的click事件,与直接点击另一个按钮效果不一样呢。
菜鸟一枚,求高手指点!!!

新建一个Web窗体
前台代码如下

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
    ViewStateMode="Disabled" %>

<!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 runat="server">
    <title></title>
    <script type="text/javascript" language="javascript">
        function window.onload() {
            // alert('111');
            alert(document.getElementById("lbl_red").innerText);
        }
        function RedColor() {
            window.document.getElementById("Button1").click();
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btn_change" runat="server" Text="变色" OnClientClick="RedColor();" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </div>
    <div>
        <asp:Label ID="lbl_red" runat="server" Width="100px" ViewStateMode="Disabled"></asp:Label>
        <br />
        <asp:Label ID="lbl_green" runat="server" Text="Green" Width="100px"></asp:Label>
        <br />
        <asp:Label ID="lbl_blue" runat="server" Text="Blue" Width="100px"></asp:Label>
    </div>
    </form>
</body>
</html>

后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void RedColor(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        this.lbl_red.Text = "***";   
      //  ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='tex