Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to fire the Click event of the linkbutton placed inside a datalist,and i'm using the following code


ASPX Page


XML
<asp:LinkButton ID="LinkButton1" runat="server" Font-Names="Bertham" Font-Size="X-Large"
                            Font-Underline="False" CommandName = "answer">"<%#Eval ("Question") %>"</asp:LinkButton>


Code Behind Page

XML
protected void DataList1_ItemCommand(object source,System.Web.UI.WebControls.DataListCommandEventArgs e)
   {
       if (e.CommandName == "answer")
   {
       Response.Write("<script language = 'javascript'>alert('U Clicked The Linkbutton'</script>)");
       Response.Write("Hello");
       //Response.Write("Hello");
   }
   }

but its not working....
Can Anyone help me. I think this is a big problem 2 many. Which I found on net while trying to sought out the problem by myself.

Thanks In Advance....
Posted
Updated 4-Jun-12 1:33am
v2

 
Share this answer
 
if you have only one condition, you can remove if , and do this on grid databound event , because when you use item command it post back and after it response you.
C#
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            LinkButton btn = (LinkButton) FindControl("lnkbtn");
            btn.Attributes.Add("onclick","alert(you are leaving this page!)");
        }
 
Share this answer
 
Comments
Arjun YK 5-Jun-12 0:22am    
Thank u for the solution.
Malu_13 26-Nov-16 5:35am    
How to use more than linkbutton in datalist.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900