Execute javascript on a DataGrid's LinkButton click?

G

Guest

Hi,

I have a datagrid with a Templated column below. I want to execute some javascript before the postback to show a hidden "div" tag with static message of "Please Wait..." since the query takes a few seconds. The js function simply sets the css style to display.

<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton Runat="server" CommandName="Select" ID="lbtnCompanyName">
<%# DataBinder.Eval(Container.DataItem, "CompanyName")%>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

The server side code to add the javascript "onclick" handler to the templated column above...

LinkButton company = (LinkButton)e.Item.Cells[0].Controls[1];
company.Attributes.Add("onclick", "getCompanyInfo()");

The resulting HTML for one of the datagrid rows....

<a id="dgCompanyList__ctl5_lbtnCompanyName" onclick="getCompanyInfo()" href="javascript:__doPostBack('dgPortfolio$_ctl5$lbtnCompanyName','')">Acme, Inc.</a>

The js function doesn't seem to get called and the postback occurs anyway. The strange thing is if I say "return getCompanyInfo()" and the function returns false, the message appears but the form submit is cancelled.

Should I execute the js function AFTER the postback is called so it's display until the page is refreshed? If so how do I get it execute after _doPostBack?

Thanks, Dave.
 
J

Joe Fallon

You can wrap the <asp:LinkButton> in a <span> control that has the
javascript. Use runat=server and Onclick=javascript, I think.
Then when you click the button the javascript runs first.
For example, I have a Delete button and I wrap in a confirm message box.
If the user hits OK then the delete is processed. But if they Cancel, then
it is not.
--
Joe Fallon



Dave said:
Hi,

I have a datagrid with a Templated column below. I want to execute some
javascript before the postback to show a hidden "div" tag with static
message of "Please Wait..." since the query takes a few seconds. The js
function simply sets the css style to display.
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton Runat="server" CommandName="Select" ID="lbtnCompanyName">
<%# DataBinder.Eval(Container.DataItem, "CompanyName")%>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

The server side code to add the javascript "onclick" handler to the templated column above...

LinkButton company = (LinkButton)e.Item.Cells[0].Controls[1];
company.Attributes.Add("onclick", "getCompanyInfo()");

The resulting HTML for one of the datagrid rows....

<a id="dgCompanyList__ctl5_lbtnCompanyName" onclick="getCompanyInfo()"
href="javascript:__doPostBack('dgPortfolio$_ctl5$lbtnCompanyName','')">Acme,
Inc. said:
The js function doesn't seem to get called and the postback occurs anyway.
The strange thing is if I say "return getCompanyInfo()" and the function
returns false, the message appears but the form submit is cancelled.
Should I execute the js function AFTER the postback is called so it's
display until the page is refreshed? If so how do I get it execute after
_doPostBack?
 
M

mimi

You need to have return in onclick event. So the "return getCompanyInfo()"
is correct, did you check in your js function to make sure there is return
true at the end.


Joe Fallon said:
You can wrap the <asp:LinkButton> in a <span> control that has the
javascript. Use runat=server and Onclick=javascript, I think.
Then when you click the button the javascript runs first.
For example, I have a Delete button and I wrap in a confirm message box.
If the user hits OK then the delete is processed. But if they Cancel, then
it is not.
--
Joe Fallon



Dave said:
Hi,

I have a datagrid with a Templated column below. I want to execute some
javascript before the postback to show a hidden "div" tag with static
message of "Please Wait..." since the query takes a few seconds. The js
function simply sets the css style to display.
<asp:TemplateColumn>
<ItemTemplate>
<asp:LinkButton Runat="server" CommandName="Select" ID="lbtnCompanyName">
<%# DataBinder.Eval(Container.DataItem, "CompanyName")%>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>

The server side code to add the javascript "onclick" handler to the templated column above...

LinkButton company = (LinkButton)e.Item.Cells[0].Controls[1];
company.Attributes.Add("onclick", "getCompanyInfo()");

The resulting HTML for one of the datagrid rows....

<a id="dgCompanyList__ctl5_lbtnCompanyName" onclick="getCompanyInfo()"
href="javascript:__doPostBack('dgPortfolio$_ctl5$lbtnCompanyName','')">Acme,
Inc. said:
The js function doesn't seem to get called and the postback occurs
anyway.
The strange thing is if I say "return getCompanyInfo()" and the function
returns false, the message appears but the form submit is cancelled.
Should I execute the js function AFTER the postback is called so it's
display until the page is refreshed? If so how do I get it execute after
_doPostBack?
Thanks, Dave.
 

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

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top