2

Hi all I have a simpe ASP button inside a grid. But the onclick event doesn't seem to fire. Where did I go wrong?

Here's the first line of my aspx page.

<%@ Page Title="Trainer Data" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site.Master" CodeFile="TrainerData.aspx.vb" Inherits="TrainerData"%>

And the button inside my gridview..

<asp:GridView ID ="gvExecSummary" runat="server" CssClass="gridview" AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" Width="98%" >
<RowStyle Height="22px" />
<AlternatingRowStyle Height="22px" CssClass="bg" BackColor="LightGray"/>
<HeaderStyle Height="22px" BackColor="#4b6c9e" Font-Bold="true"/>
<Columns>
<asp:TemplateField  HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"     OnClientClick="btnExecutiveGenerate_Click" />
</ItemTemplate>
</asp:TemplateField>

P.S. I tried even onclick but it doesn't work either.

EDIT: My code for server side.

Protected Sub btnExecutiveGenerate_Click(sender As Object, e As EventArgs)
    Dim gvrow As GridViewRow = CType(CType(sender, Control).Parent.Parent, GridViewRow)
    Dim lblSchoolId As System.Web.UI.WebControls.Label = gvrow.FindControl("lblSchoolMasterID")
    Dim lblFacultyId As System.Web.UI.WebControls.Label = gvrow.FindControl("lblFacultyMasterID")
    Dim btnExecutiveGenerate As System.Web.UI.WebControls.Button = gvrow.FindControl("btnExecutiveGenerate")

    PDF_Creation_Executive(Val(lblSchoolId.Text), Val(lblFacultyId.Text))

End Sub
4
  • Do you have the code for btnExecutiveGenerate_Click? If so, could you post it here too? Commented Aug 30, 2013 at 7:56
  • Hi, pl check my edit. Commented Aug 30, 2013 at 8:10
  • @Aishvarya how you load data to gridview ? are you doing it page load method? can you please update with that code? Commented Aug 30, 2013 at 8:27
  • Hi, I load the grid on selected index changed of a dropwdown and here is the code.. Dim dbl As New vtisDAL.vtisDALDataContext Dim dsGetFacDtls = From dF In dbl.usp_GetFacultyDetails(0).AsQueryable Where dF.SchoolMasterID = Val(CboSubSchoolSel.SelectedValue) Select dF Order By dF.SchoolID, dF.FacultyID gvExecSummary.DataSource = dsGetFacDtls gvExecSummary.DataBind() Commented Aug 30, 2013 at 8:49

4 Answers 4

3

Use Command Argumnet,

<asp:TemplateField>
<ItemTemplate>
  <asp:Button ID="AddButton" runat="server" 
  CommandName="AddToCart" 
  CommandArgument="<%# CType(Container,GridViewRow).RowIndex %>"
  Text="Add to Cart" />
   </ItemTemplate> 
</asp:TemplateField>

add code page side

Protected Sub GridView1_RowCommand(ByVal sender As Object, _ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
 If (e.CommandName = "AddToCart") Then
  ' Retrieve the row index stored in the CommandArgument property.
  Dim index As Integer = Convert.ToInt32(e.CommandArgument)

  ' Retrieve the row that contains the button 
  ' from the Rows collection.
  Dim row As GridViewRow = GridView1.Rows(index)

  ' Add code here to add the item to the shopping cart.

 End If
End Sub
Sign up to request clarification or add additional context in comments.

Comments

1

You need to handle the button click event of Gridview in RowCommand event

NOTE: Please see the CommandName & CommandArgument properties added to the button.

<asp:GridView ID ="gvExecSummary" runat="server" CssClass="gridview" AllowSorting="false" AllowPaging="false" AutoGenerateColumns="false" Width="98%" >
<RowStyle Height="22px" OnRowCommand="gvExecSummary_RowCommand"  />
<AlternatingRowStyle Height="22px" CssClass="bg" BackColor="LightGray"/>
<HeaderStyle Height="22px" BackColor="#4b6c9e" Font-Bold="true"/>
<Columns>
<asp:TemplateField  HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="5%" HeaderText="Action">
<ItemTemplate>
<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"   CommandName="GenerateExecutive" CommandArgument="<%#((GridViewRow)Container).RowIndex %>"  />
</ItemTemplate>
</asp:TemplateField>

And and RowCommand event will be..

protected void gvExecSummary_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
      if (e.CommandName == "GenerateExecutive") 
      {
          // button click code goes here
      }
}

5 Comments

My "e" is of type GridViewRowEventArgs so I am not able to access CommandName property.
@Aishvarya - Could you pleae elaborate? The EventArgs on the handler of GridView OnRowCommand HAS to be of type GridViewCommandEventArgs . And this would have the CommandName and CommandArgument properties available.
I tried using this. Think the code is bewitched, does not work at all.
As you see, this is C# code. I have tried this before posting it. Could you please share what error you get?
0

change OnClientClick event to OnClick if btnExecutiveGenerate_Click is vb.net event handler

<asp:Button ID="btnExecutiveGenerate" runat="server" Text="Generate"    
 OnClick="btnExecutiveGenerate_Click"/>

OnClientClick event use to execute client-side script, if you have given OnClientClick event with OnClick event, then if OnClientClick return true only it will call OnClick event.

so make sure you are returning true or false from OnClientClick event if you using it.

note that if you are loading data in page laod, do as below

Sub Page_Load
    If Not IsPostBack
        LoadGridViewData()
    End If
End Sub

1 Comment

He just said > P.S. I tried even onclick but it doesn't work either.
0

This may help someone, but I was experiencing edit buttons not firing the event within rows (and not footer). The cause was a gridview containing a label, which had the same ID as one elsewhere on the page. Seemingly this did not cause problems for the footer row, which did not contain any such labels. I hope this helps someone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.