ASP.NET - rowcommand event not firing after a postback in c# / ASP.net

Asked By muthuraman alexander on 18-Sep-11 01:56 AM

hi all,

i having agrid view and a drop down box inside the updatepanel
when i  select an item from  the drop down the grid have to get filter...this is working for me

then also i hav a image button in the gridview template field ...when i click the imagebutom i ll generate new row in the grid

dusing the first page load it working well
when i select the dropdown it ll populate the new data  again in the grid ...now the image button was not working ..and not firing the rowCommand event ...

i tried this inside 2 diff update panel,,,without update pannel ,,with one update panel ..but it was not working
also i tried by using async triggers inside the update pannel triggers ..no use in that

please help me ...here i posted the aspx code for this page


code snipshot
--------------------------------------------
<
div class="pageHeader">

<div class="caption">

<h1>

<asp:Localize ID="Caption" runat="server" Text="Shipping Export"></asp:Localize></h1>

</div>

</div>

<input type="file" runat="server" id="file1" style="display: none" />

<div style="margin-left: 4px; margin-right: 4px;">

<asp:HiddenField ID="hdnFilePath" runat="server" />

<table cellpadding="2" cellspacing="0" class="innerLayout">

<tr>

<!-- SEARCH By Criteria -->

<td style="padding: 10px 2px 10px 0px; width: 250px; vertical-align: top; text-align: left;">

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

<asp:Panel ID="SearchPanel" runat="server" CssClass="section" EnableViewState="false">

<div class="header">

<h2 class="searchthiscategory">

<asp:Localize ID="SearchCaption" runat="server" Text="Search Criteria" EnableViewState="false"></asp:Localize></h2>

</div>

<div class="content">

<table cellpadding="4" cellspacing="0" class="inputForm">

<tr>

<td colspan="6">

<asp:Panel ID="BasicSearchPanel" runat="server">

<asp:Localize ID="BasicSearchHelpText" runat="server" Text="Set the criteria below, then click <b>Search</b> to filter the order listing. To provide more specific criteria, try the "></asp:Localize>

</asp:Panel>

</td>

</tr>

<tr>

<th class="rowHeader">

<asp:Label ID="StatusFilterLabel" runat="server" Text="Status:"></asp:Label>

</th>

<td>

<asp:DropDownList ID="drpStatusFilter" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drpStatusFilter_SelectedIndexChanged">

<asp:ListItem Text="select" Value="4" Enabled="false"></asp:ListItem>

<asp:ListItem Text="ALL" Value="0"></asp:ListItem>

<asp:ListItem Text="Already Shipped" Value="3"></asp:ListItem>

<asp:ListItem Text="Already Exported" Value="2"></asp:ListItem>

<asp:ListItem Text="Not Yet Created" Value="1"></asp:ListItem>

</asp:DropDownList>

</td>

</tr>

<tr>

<th class="rowHeader">

<asp:Label ID="PaymentStatusFilterLabel" runat="server" Text="View By:"></asp:Label>

</th>

<td>

<asp:DropDownList ID="drpViewFilter" runat="server" AutoPostBack="true" OnSelectedIndexChanged="drpViewFilter_SelectedIndexChanged">

<asp:ListItem Text="select" Value="2" Enabled="false"></asp:ListItem>

<asp:ListItem Text="Group-By View" Value="0"></asp:ListItem>

<asp:ListItem Text="Default View" Value="1"></asp:ListItem>

</asp:DropDownList>

</td>

</tr>

</table>

</div>

</asp:Panel>

</ContentTemplate>

</asp:UpdatePanel>

</td>

<!-- SEARCH By Criteria END -->

<!-- Grid Resultant Order Items -->

<td style="padding: 10px 0px 10px 0px; vertical-align: top; text-align: left;">

<div class="section">

<div class="content">

<!-- Main Grid for All Order Items -->

<table cellpadding="4" cellspacing="0" width="100%">

<tr>

<!-- Error -->

<div id="divErrorNew" runat="server" class="error" style="display: none">

<img src="../../images/error_icon.jpg" style="float: left; padding-left: 5px;" />

<div id="divError" runat="server" class="errorIn">

</div>

</div>

</tr>

<tr>

<td style="vertical-align: top; text-align: right;">

<asp:Button ID="Button1" Text="Export CSV" OnClick="ExportCSV_Click" runat="server" />

</td>

</tr>

<tr>

<td>

<!-- Group By Grid for already Shipped and Exported -->

<div id="div_GrbBy" runat="server">

<table cellpadding="4" cellspacing="0" width="100%">

<tr>

<td>

<ajax:UpdatePanel ID="SearchResultAjax" runat="server" UpdateMode="Conditional">

<ContentTemplate>

<asp:GridView ID="gridNotExported" runat="server" SkinID="PagedList" Width="100%"

AllowPaging="false" AutoGenerateColumns="False" DataKeyNames="OrderId" OnRowCommand="gridRowCommand"

OnRowDataBound="gridDataBound" OnSorting="gridNotExported_Sorting">

<Columns>

<asp:BoundField DataField="OrderId" HeaderText="OrderID" ItemStyle-HorizontalAlign="Center"

ItemStyle-Width="50px" SortExpression="OrderId" />

<asp:BoundField DataField="Customer" HeaderText="Customer" ItemStyle-HorizontalAlign="Center"

ItemStyle-Width="100px" />

<asp:TemplateField HeaderText="Weight">

<ItemStyle HorizontalAlign="Right" Height="30px" Width="100px" />

<ItemTemplate>

<asp:TextBox ID="grdTxtWeight" value="0" Text='<%#Bind("Weight") %>' runat="server"

Width="50px" OnKeyDown="isNumeric(event)"></asp:TextBox>

<asp:Label ID="grdLblWeight" Text='<%#Bind("Weight") %>' runat="server" Visible="false"></asp:Label>

<label>

Kg</label>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField HeaderText="Insurance">

<ItemStyle HorizontalAlign="Left" Height="30px" Width="100px" />

<ItemTemplate>

<table>

<tr>

<td style="width: 50px">

<label>

$</label>

<asp:Label ID="grdLblInsurance" runat="server" Text='<%#Bind("Insurance") %>' />

</td>

<td>

<asp:CheckBox ID="grdChkInsurance" Checked="false" runat="server" />

</td>

</tr>

</table>

</ItemTemplate>

</asp:TemplateField>

<asp:TemplateField>

<ItemStyle HorizontalAlign="Center" Height="30px" Width="10px" />

<ItemTemplate>

<table>

<tr>

<td>

<asp:Image ID="grdStatusImg" runat="server" Height="16px" Width="16px" />

</td>

<td>

<asp:ImageButton ID="grdImgBtn" runat="server" CommandName="PLUS" CommandArgument="PLUS"

ImageUrl="~/images/plus.gif" />

</td>

</tr>

</table>

<asp:HiddenField ID="hdnTotalPrice" runat="server" Value='<%#Bind("TotalCharges") %>' />

<asp:HiddenField ID="hdnOrderStatus" runat="server" Value='<%#Bind("Status") %>' />

<asp:HiddenField ID="hdnType" runat="server" Value='<%#Bind("orderType") %>' />

</ItemTemplate>

</asp:TemplateField>

</Columns>

<EmptyDataTemplate>

<asp:Label ID="EmptyMessage" runat="server" Text="No orders match criteria."></asp:Label>

</EmptyDataTemplate>

</asp:GridView >

</ContentTemplate>

<Triggers>

<ajax:AsyncPostBackTrigger ControlID="gridNotExported" EventName="gridRowCommand" />

</Triggers>

</ajax:UpdatePanel>

</td>

</tr>

<tr>

<td>

<br />

&nbsp;

</td>

</tr>

<tr>

<td style="vertical-align: top; text-align: right;">

<asp:Button ID="btnExportCSV" Text="Export CSV" OnClientClick="javascript:getFile();"

OnClick="ExportCSV_Click" runat="server" />

</td>

</tr>

</table>

</div>

</div>

</td>

<!-- Grid Resultant Order Items END-->

</tr>

</table>

</div>

-------------------------------------------------

Any recommendations ,ideas ,help appreciated
i need help for this task

thanks in advance
MUTHU

smr replied to muthuraman alexander on 18-Sep-11 02:05 AM
hi

here is the solution

refer the link below

http://stackoverflow.com/questions/2092954/event-not-firing-after-setting-onclientclick-in-rowdatabound
TSN ... replied to muthuraman alexander on 19-Sep-11 12:06 AM
hi..

the problem may due to this property...

enter a property of the asp control called "CausesValidation" to false.

Setting this will remove that button from checking that are the validation requirements fulfilled on the page and thus it will cause a postback.



hope this helps you
Jitendra Faye replied to muthuraman alexander on 19-Sep-11 12:37 AM
For that first you have to implement GridView RowCommand event.

Follow these steps-

Step1. Create your page like this-

<asp:gridview id="ContactsGridView"

datasourceid="ContactsSource"

allowpaging="true"

autogeneratecolumns="false"

onrowcommand="ContactsGridView_RowCommand"

runat="server">

<columns>

<asp:buttonfield buttontype="Link"

commandname="Add"

text="Add"/>

<asp:boundfield datafield="ContactID"

headertext="Contact ID"/>

<asp:boundfield datafield="FirstName"

headertext="First Name"/>

<asp:boundfield datafield="LastName"

headertext="Last Name"/>

</columns>

</asp:gridview>


Step2- Implement RowCommand Event like this-

void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)

{

// If multiple buttons are used in a GridView control, use the

// CommandName property to determine which button was clicked.

if (e.CommandName == "Add")

{

// Convert the row index stored in the CommandArgument

// property to an Integer.

int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked

// by the user from the Rows collection.

GridViewRow row = ContactsGridView.Rows[index];

// Create a new ListItem object for the contact in the row.

ListItem item = new ListItem();

item.Text = Server.HtmlDecode(row.Cells[2].Text) + " " +

Server.HtmlDecode(row.Cells[3].Text);

// If the contact is not already in the ListBox, add the ListItem

// object to the Items collection of the ListBox control.

if (!ContactsListBox.Items.Contains(item))

{

ContactsListBox.Items.Add(item);

}

}

}



Try this and let me know.
Radhika roy replied to muthuraman alexander on 20-Sep-11 11:46 AM

Implement GridView rowCopmmand like this-

.aspx
page-
 
<asp:GridView id="ProductsGridView"

        DataSourceID="ProductsDataSource"
        DataKeyNames="ProductID"
        AllowPaging="True" 
        OnRowCommand="ProductsGridView_RowCommand"
        AutoGenerateColumns="False"
        runat="server">
        <Columns>
          <asp:BoundField DataField="Name" HeaderText="Product Name" />
          <asp:BoundField DataField="ProductNumber" HeaderText="Product Number" />          
          <asp:TemplateField HeaderText="Price">
            <ItemTemplate>
              <asp:Label ID="PriceLabel" runat="server" 
                Text='<%# Bind("ListPrice")
%>'>
              </asp:Label>
            </ItemTemplate>
          </asp:TemplateField>
          <asp:TemplateField>
            <ItemTemplate>                
              <asp:Button runat="server" ID="IncreaseButton"
                Text="Increase Price
5%"
                CommandName="Increase"
                CommandArgument="<%# ((GridViewRow) Container).RowIndex
%>" />
            </ItemTemplate>
          </asp:TemplateField>
        </Columns>
      </asp:GridView>

.cs page-

void
ProductsGridView_RowCommand(Object sender, GridViewCommandEventArgs
e)
  {
    // If multiple buttons are used in a
GridView control, use the
    // CommandName property to determine which
button was clicked.
    if(e.CommandName=="Increase")
    {
      // Convert the row index stored in the
CommandArgument
      // property to an
Integer.
      int index =
Convert.ToInt32(e.CommandArgument);

      // Retrieve the row that contains the button
clicked 
      // by the user from the Rows collection.    
 
      GridViewRow row = ProductsGridView.Rows[index];

      // Calculate the new
price.
      Label listPriceTextBox = (Label)row.FindControl("PriceLabel");
      listPriceTextBox.Text = (Convert.ToDouble(listPriceTextBox.Text) *
1.05).ToString();     

      // Update the
row.
      ProductsGridView.UpdateRow(index, false);
    }
  }



Hope this will help you.