How to make event fire once?

E

erdos

I have an asp.net 2.0 button with server and client side click
events. On the first click, both events will fire. On the next
click, only the client side event fires. How do I make the server
side event not fire after the first click?

Thanks,
Brett
 
G

Guest

Could you explain a little bit more what you're trying to do? Should it
prevent the postback or just not to fire server side event handler? Paste
some code...
 
E

erdos

Could you explain a little bit more what you're trying to do? Should it
prevent the postback or just not to fire server side event handler? Paste
some code...

Here's the scenario: ControlA.visible = false loads initially.
ControlA will load much data once the user clicks Button1. So I'm
only loading data on demand. At some point, the user decides they
want to view that data and clicks Button1. This fires Button1_Click()
server side and loads ControlA and sets ControlA.visible=true. The
client side event fires and changes a DIV to visibility="block", which
ControlA sits in. Now ControlA is visible. Next the user clicks
Button2, which hides ControlA client side and displays ControlB. The
user clicks Button1, which client side hides ControlB and displays
ControlA but does not hit the server again, since ControlA is already
loaded with data.

I may have three buttons and each toggles visibility of a control but
only one control displays at a time. I don't need to load data into
the controls after they are initially displayed. That's what I want
to prevent; hitting the server again and again, since there isn't any
need. If I keep hitting the server, I'll just be loading the same
data over and over. It only needs to load on that first click for its
respective button.
 
G

Guest

Howdy,

OK, i see. Please find example below

-- begin snippet --

<asp:panel runat="server" ID="panelA">
i'm the default panel
<asp:Button runat="server" ID="btnA" Text="Switch to Panel B"
OnClick="btnA_Click" />
</asp:panel>
<asp:panel runat="server" ID="panelB" Visible="false">
<asp:DataList runat="server" ID="hugeList">
<ItemTemplate>
some data
<%# Container.ItemIndex %>
</ItemTemplate>
</asp:DataList>
<asp:Button runat="server" ID="btnB" Text="Switch to Panel A"
OnClientClick="SwitchPanel(0); return false" />
</asp:panel>

<script runat="server">
protected void btnA_Click(object sender, EventArgs e)
{

panelB.Visible = true;
panelA.Style["display"] = "none";

// prevent button from perfoming postback
Button button = (Button)sender;
button.OnClientClick = "SwitchPanel(1); return false;";

hugeList.DataSource = new int[10];
hugeList.DataBind();
}
</script>

<script type="text/javascript">
//<!--
function SwitchPanel(arg)
{
var panelA = document.getElementById('<%=panelA.ClientID %>');
var panelB = document.getElementById('<%=panelB.ClientID %>');

if (arg == 0)
{
panelA.style.display = 'block';
panelB.style.display = 'none'
}
else
{
panelA.style.display = 'none';
panelB.style.display = 'block'
}
}
//-->
</script>

-- end snippet --
 
E

erdos

Thanks Milosz. This is close to what I'm doing now but I still don't
see how your code prevents postbacks. Initially, your button has a
server side click_event wired up. Then you give it a onClientClick.
But the server side click_event is still there. This is what I have
now but both always fire.

If a client click_event is present, is that supposed to prevent a
server side client_event from firing? If so, it isn't working for me.
 
G

Guest

Howdy,

have you run it? i tested it and it's working as you wanted :) i prevent
postback by adding
OnClientClick= "SwitchPanel(); return false;"
"return false;" causes any other javascript code attached to onclick
javascript event won't be executed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top