Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a DataList and inside the SelectedItemTemplate there is a button.
<asp:Button ID="getDetails" runat ="server" Text ="Send Enquiry" CommandName ="SendEnquiry" Font-Names ="verdana" Font-Size ="8pt" Height ="20px" Width ="90px" />
When this button is clicked I should know wheather the event is raised by this button. For this purpose I had written the following code.

C#
protected void carList_ItemCommand(object sender, DataListCommandEventArgs  e)
   {
       try
       {
           if(e.CommandName =="SendEnquiry")
           {
               Label12.Text = "Send Enquiry";
           }
           else
           {
               Label12.Text ="Do not send";
           }
       }
       catch (Exception e1)
       {
           Label12.Text = e1.Message.ToString();
       }
    }


But when I click this button the above condition is never becoming true. Can anyone tell what is wrong.

Thanks in advance!
Posted

first thing i would like to know is why you need to keep button in datalist.
what is your purpose behind doing that
 
Share this answer
 
Comments
Soumini Ramakrishnan 8-Sep-10 2:39am    
I have 3 textboxes inside the SelectedItemTemplate. I have to take the values of these textboxes and do some process when this button is clicked.
Convert the Asp:Button into an Asp:LinkButton. It will work.

That is:

<asp:LinkButton ID="getDetails" runat="server" Text="Send Enquiry" CommandName="SendEnquiry" Font-Names="verdana" Font-Size="8pt" Height="20px" Width="90px" />


Or, you have to use the UseSubmitBehavior="false" property for the Asp:Button, to make it work. That is:
<asp:Button ID="getDetails" runat="server" Text="Send Enquiry" UseSubmitBehavior="false" CommandName="SendEnquiry" Font-Names="verdana" Font-Size="8pt" Height="20px" Width="90px" />


The reason is, by default an Asp:Button renders an Submit button and DataList doest not work if the button has a Submit behaviour. So, you need to mark the UseSubmitBehavior="false"


Good luck :)
 
Share this answer
 
v3
Comments
Soumini Ramakrishnan 8-Sep-10 2:44am    
this is working.But I couldn't take the values of textboxes which are inside this selectedItemTemplate using the following code:

if(e.CommandName =="SendEnquiry")
{
TextBox t = (TextBox)e.Item.FindControl("userName");

Label12.Text = t.Text.ToString () +"Send Enquiry";
}
Soumini Ramakrishnan 8-Sep-10 3:03am    
When giving UseSubmitBehavior="false", it is working. But I couldn't get values from textboxes
Al-Farooque Shubho 8-Sep-10 3:34am    
Yes, because, this time the values are not being submitted to the server. I would better suggest you to use LinkButton, instead of the Button.

See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.edititemtemplate.aspx

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