Question : OnClientClick Needs PostBack

I have a linkButton that needs to perform serverside stuff.

But i want to prevent user from clicking more than once.

When I disable the button with javascript, the postback is cancelled.

i want both, disable and postback.

see attached snippets
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
default.aspx
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
    <style type="text/css">
      .style100
      {
        width: 100%;
      }
      .style20
      {
        width: 20%;
      }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <table class="style100">
      <tr class="style100">
        <td class="style20">&nbsp;</td>
        <td class="style20">
          &nbsp;</td>
        <td class="style20">
          &nbsp;</td>
        <td class="style20">
          &nbsp;</td>
        <td class="style20">&nbsp;</td>
      </tr>
      <tr class="style100">
        <td class="style20">
          <asp:LinkButton 
                    ID="lbBuildProjection" 
                    runat="server" 
                    onclick="lbBuildProjection_Click" 
                    Text="Build Projections" 
                    ToolTip="Project Targeting $" 
                    OnClientClick="AllowClickOnce('lbBuildProjection')"
                    ></asp:LinkButton>
                    </td>
        <td class="style20">
          <asp:CheckBox ID="cbBuildProjection" runat="server" Text="Enabled" />
        </td>
        <td class="style20">
          <asp:Label ID="lblBuildProjections" runat="server"></asp:Label>
        </td>
        <td class="style20">
          <asp:Button ID="btnReEnable" runat="server" onclick="btnReEnable_Click" Text="btnReEnable" />
        </td>
        <td class="style20">&nbsp;</td>
      </tr>
      <tr class="style100">
        <td class="style20">&nbsp;</td>
        <td class="style20">
          &nbsp;</td>
        <td class="style20">&nbsp;</td>
        <td class="style20">&nbsp;</td>
        <td class="style20">&nbsp;</td>
      </tr>
      </table>
    <div>
    
    </div>
    </form>
    <script src = "AgmaJScript.js" type = "text/jscript" ></script>
</body>
</html>

default.aspx.cs

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)
    {
        cbBuildProjection.Checked = lbBuildProjection.Enabled;
        lblBuildProjections.Text = "Open";
    }
    protected void btnReEnable_Click(object sender, EventArgs e)
    {
        lbBuildProjection.Enabled = true;
        lblBuildProjections.Text = "Open";
    }
    protected void lbBuildProjection_Click(object sender, EventArgs e)
    {
        cbBuildProjection.Checked = lbBuildProjection.Enabled;
        lblBuildProjections.Text = "Clicked";
    }
}


Javascript 
AgmaJScript.js.js
function AllowClickOnce(ctl) {
    javascript:
    var linkbutton = document.getElementById(ctl);
    if (linkbutton.disabled != true) {
        linkbutton.disabled = true;
        return true;
    }
    else {
        return false;
    }
}

Answer : OnClientClick Needs PostBack

1. Free range grazing is land intensive (needs lots of space).

2. Requires herding, which is expensive.

3. Uneven diet would affect the quality of milk.

I am sure I can list another 6 reasons why it is impractical.

Random Solutions  
 
programming4us programming4us