Execute javascript on a DataGrid's LinkButton click?

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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?
 
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.
 
Back
Top