使用OnClientClick和onClick不应该刷新页面

时间:2013-07-22 14:05:56

标签: c# javascript asp.net button xhtml

我正在尝试使用onclient单击onclient。问题是页面在运行任何javascript代码之前刷新。我正在尝试将我的javascript代码集成到其他人的代码中。我知道他的代码有很多不好的编码实践,但我需要使用它。如果你想让我更多地隔离代码,我也可以这样做。我已经尝试删除onclick和onclientclick。该页面仍然刷新。

aspx文件                          在线检查                                 

    

<center>
<h2>
    Alvey Quality Monitoring Scheme</h2></center>
    <div>
        <h3>
            Every 1/2 hour, check a unit from each line for the following: &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; Line 1 &nbsp;&nbsp; Global ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            Line 2 &nbsp; Global ID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp;&nbsp; 
            Lines Running&nbsp;</h3>
        <h3>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<asp:DropDownList ID="DropdownList1" runat="server" Height="20px" Width="85px" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="intSKU" DataValueField="strSpec" AppendDataBoundItems="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
            <asp:ListItem Text="Select" Value="0" />
            </asp:DropDownList><asp:TextBox ID="TextBox1" runat="server" Height="20px" Width="63px" ReadOnly="True"></asp:TextBox><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Nestle1ConnectionString2 %>"
                SelectCommand="SELECT [intSKU], [strSpec] FROM [lkpSKUInfo] ORDER BY [intSKU]"></asp:SqlDataSource>

            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="DropdownList1"></asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="DropdownList2"></asp:RequiredFieldValidator>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="DropDownList3"></asp:RequiredFieldValidator>


            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<asp:DropDownList ID="DropdownList2" runat="server"
                Height="20px" Width="85px" AutoPostBack="True" DataSourceID="SqlDataSource1" AppendDataBoundItems="true" DataTextField="intSKU" DataValueField="strSpec" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
             <asp:ListItem Text="Select" Value="0" />
            </asp:DropDownList><asp:TextBox ID="TextBox2" runat="server" Height="20px" Width="63px" ReadOnly="True"></asp:TextBox>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Line 1 &nbsp;
            <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged1" Checked="True" />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Line 2&nbsp;<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox2_CheckedChanged" AutoPostBack="True" Checked="True" />
            &nbsp;</h3>

        <h3>
            &nbsp; &nbsp;&nbsp;
            <asp:Label ID="Label1" runat="server" Height="28px" Text="Line 1 Label View " Width="148px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
            <asp:Label ID="Label2" runat="server" Height="28px" Text="Line 2 Label View" Width="148px"></asp:Label></h3>
        <p>
            &nbsp; &nbsp;<asp:Image ID="Image1" runat="server" Height="155px" Width="670px" />
            <asp:Image ID="Image2" runat="server" Height="155px" Width="670px" /></p>
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
        <h3>
            &nbsp; &nbsp;
            <asp:Label ID="Label3" runat="server" Height="28px" Text="Line 1 Print Apply View"
                Width="219px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp;<asp:Label ID="Label4" runat="server" Height="28px" Text="Line 2 Print Apply View"
                Width="207px"></asp:Label>&nbsp;</h3>
        <h3>
            &nbsp; &nbsp;<asp:Image ID="Image3" runat="server" Height="155px" Width="670px" />
            <asp:Image ID="Image4" runat="server" Height="155px" Width="670px" />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</h3>
            <h3>
            &nbsp;<asp:Label ID="Label6" runat="server" Height="17px" Text="CASE CODE:   " Width="133px"></asp:Label>
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
                &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
                <asp:Label ID="Label9" runat="server" Height="17px" Text="Line 1"
                    Width="108px"></asp:Label>
                &nbsp; &nbsp; &nbsp; &nbsp;
                <asp:Label ID="Label10" runat="server" Height="17px" Text="Line 2"
                    Width="108px"></asp:Label></h3>
        <h3>
            &nbsp; &nbsp;<asp:Label ID="Label5" runat="server" Height="39px" Text="Case code must match the line and hour from the can code"
                Width="904px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:CheckBox ID="CheckBox3" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox3_CheckedChanged" Checked="True" />
            &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox4" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox4_CheckedChanged" Checked="True" /></h3>

        <h3>
            &nbsp;<asp:Label ID="Label7" runat="server" Height="17px" Text="SHRINK FILM" Width="122px"></asp:Label>&nbsp;</h3>
        <h3>
            &nbsp; &nbsp;<asp:Label ID="Label8" runat="server" Height="39px" Text="Shrink Film must be intact, tightly covering the entire case, and have bullseyes that are tight and will not allow a can to fall out" Width="904px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:CheckBox ID="CheckBox5" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox5_CheckedChanged" Checked="True" />
            &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox6" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox6_CheckedChanged" Checked="True" /></h3>
        <h3>
            &nbsp;<asp:Label ID="Label11" runat="server" Height="17px" Text="LABELS"
                Width="122px"></asp:Label>&nbsp;</h3>
        <h3>
            &nbsp; &nbsp;<asp:Label ID="Label12" runat="server" Height="39px" Text="Labels must be correct for SKU scheduled to be produced, be applied correctly to the cans with no loose, crooked or upside-down labels" Width="904px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:CheckBox ID="CheckBox7" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox7_CheckedChanged" Checked="True" />
            &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox8" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox8_CheckedChanged" Checked="True" /></h3>
        <h3>
            &nbsp;<asp:Label ID="Label13" runat="server" Height="17px" Text="TRAYS"
                Width="122px"></asp:Label></h3>
        <h3>
            &nbsp; &nbsp;<asp:Label ID="Label14" runat="server" Height="39px" Text="Trays must match product produced, no double case, no loose flaps and no damaged cases" Width="904px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:CheckBox ID="CheckBox9" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox9_CheckedChanged" Checked="True" />
            &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox10" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox10_CheckedChanged" Checked="True" /></h3>
        <h3>
            &nbsp;<asp:Label ID="Label15" runat="server" Height="17px" Text="PRINT & APPLY"
                Width="166px"></asp:Label></h3>
        <h3>
            &nbsp; &nbsp;<asp:Label ID="Label16" runat="server" Height="39px" Text="Print & Apply stickers must match SKU produced, Julian Date, Best Before Date, Line and Time. Must be legible and applied correctly to the case. Stickers must match DIS ticket (except Australia)" Width="904px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:CheckBox ID="CheckBox11" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox11_CheckedChanged" Checked="True" />
            &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox12" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox12_CheckedChanged" Checked="True" /></h3>
        <h3>
            &nbsp;<asp:Label ID="Label17" runat="server" Height="17px" Text="DIS TAG"
                Width="166px"></asp:Label></h3>
        <h3>
            &nbsp; &nbsp;<asp:Label ID="Label18" runat="server" Height="39px" Text="Tags must match schedule and product produced" Width="904px"></asp:Label>
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:CheckBox ID="CheckBox13" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox13_CheckedChanged" Checked="True" />
            &nbsp; &nbsp; &nbsp;<asp:CheckBox ID="CheckBox14" runat="server" Text="OK/Defective" OnCheckedChanged="CheckBox14_CheckedChanged" Checked="True" /></h3><br />
          <h3>
    &nbsp; &nbsp; Comments: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
               &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
               &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Operator Number
               <asp:DropDownList ID="DropDownList3" runat="server" AppendDataBoundItems="true" Height="20px" Width="72px" DataSourceID="SqlDataSource2" DataTextField="intOperatorNumber" DataValueField="strLast">
               <asp:ListItem Text="Select" Value="No Operator Selected" />
               </asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:Nestle1ConnectionString2 %>"
                   SelectCommand="SELECT [intOperatorNumber], [strLast] FROM [tblOperators] ORDER BY [intOperatorNumber]">
               </asp:SqlDataSource>
               &nbsp; &nbsp; &nbsp;Shift &nbsp;<asp:Label ID="Label" runat="server" Height="20px" Width="53px"></asp:Label>
               &nbsp; &nbsp; &nbsp;
           </h3>
           </div>
       &nbsp;

           <asp:TextBox ID="TextBox3" runat="server" Height="81px" Width="381px"></asp:TextBox>

    <asp:Label ID="lbl"  runat="server" Height="20px" Width="80" BorderWidth="0px" Font-Names="Verdana"></asp:Label>
<input type="text" id="clock"  style="border: 0px; width: 80px; height: 20px;" value="" readonly="readonly" />

&nbsp;

代码的特定部分无效

     <!-- the button I'm clicking-->  

        <asp:Button ID="Button3"  Font-Size="15" runat="server" Text="Send Data" Height="40px" Width="245px" OnClientClick="delayer();return false;" OnClick="Button3_Click" />
        <asp:Button ID="Button4"  Font-Size="15" runat="server" Text="Back" Height="40px" Width="245px" OnClick="Button4_Click" />
    </form>


<script type="text/javascript">

</script>

    <script type="text/javascript">

    /*
 var executionTime;
 var initialTime = localStorage.getItem("initialTime");

 function foo()
 {
 if(!(initialTime === null)){

    executiontime = 5000-(new Date()).getTime() - parseInt(initialTime, 10);
    if (executionTime<0) executionTime = 0;
    showPopUp(executionTime);
 }
 }
 */


 function showPopUp( var executionTime){
 /* if(initialTime=== null)
  {
    executionTime = 5000;
  }
  localStorage.setItem("initialTime", (new Date()).getTime());
  setTimeout(function() {alert("Warning");
  localStorage.setItem("initialTime", null);}, executionTime);
 */

 alert("warning");
 }

  function delayer(){
  showPopUp();
  }


  // constants to define the title of the alert and button text.
var ALERT_TITLE = "Oops!";
var ALERT_BUTTON_TEXT = "Ok";

// over-ride the alert method only if this a newer browser.
// Older browser will see standard alerts
if(document.getElementById) {
    window.alert = function(txt) {
        createCustomAlert(txt); //overrides alert method
    }
}

function createCustomAlert(txt) {
    // shortcut reference to the document object
    d = document;

    // if the modalContainer object already exists in the DOM, bail out.
    if(d.getElementById("modalContainer")) return;

    // create the modalContainer div as a child of the BODY element
    mObj = d.getElementsByTagName("body")[0].appendChild(d.createElement("div"));
    mObj.id = "modalContainer";
     // make sure its as tall as it needs to be to overlay all the content on the page
    mObj.style.height = document.documentElement.scrollHeight + "px";

    // create the DIV that will be the alert 
    alertObj = mObj.appendChild(d.createElement("div"));
    alertObj.id = "alertBox";
    // MSIE doesnt treat position:fixed correctly, so this compensates for positioning the alert
    if(false) alertObj.style.top = document.documentElement.scrollTop + "px";
    // center the alert box
    alertObj.style.left = (d.documentElement.scrollWidth - alertObj.offsetWidth)/2 + "px";
    // create an H1 element as the title bar
    h1 = alertObj.appendChild(d.createElement("h1"));
    h1.appendChild(d.createTextNode(ALERT_TITLE));

    // create a paragraph element to contain the txt argument
    msg = alertObj.appendChild(d.createElement("p"));
    msg.innerHTML = txt;

    // create an anchor element to use as the confirmation button.
    btn = alertObj.appendChild(d.createElement("a"));
    btn.id = "closeBtn";
    btn.appendChild(d.createTextNode(ALERT_BUTTON_TEXT));
    btn.href = "#";
    // set up the onclick event to remove the alert when the anchor is clicked
    btn.onclick = function() { removeCustomAlert();return false; }


}

// removes the custom alert from the DOM
function removeCustomAlert() {
    document.getElementsByTagName("body")[0].removeChild(document.getElementById("modalContainer"));
}

 </script>


   </body>             
</html>

1 个答案:

答案 0 :(得分:1)

您需要从delayer函数返回false,并且还要返回OnClientClick事件。

<asp:Button ID="Button3"  runat="server" Text="Send Data" 
   OnClientClick="return delayer();" OnClick="Button3_Click" />

function delayer(){
  showPopUp();
  return false;
}