Click here to Skip to main content
14,827,050 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
guyz i am badly stuck here, i have a drowpdownlist in gridview on edit template and want to rais its selectedindexchanged event to get the new selected index value.
when i select any item from the dropdown, page posts back and then nothing happens.
panel which contains grid is showing by ajax modalpopupextender.

here is my .aspx code

XML
<asp:Panel ID="OrderPanel" runat="server" CssClass="Contents" Height="325px" Width="600px"
            Style="display: none">
            <asp:UpdatePanel ID="OrderDetailPanel" runat="server" UpdateMode="Conditional">
                <ContentTemplate>
                    <div class="LeftAlign">
                        <div id="InnerDiv" class="RirghAlign">
                            <asp:Label runat="server" ID="IdLable" Text="Id: " CssClass="Lable"></asp:Label>
                            <asp:TextBox runat="server" ID="IdTextBox" CssClass="TextBox"> </asp:TextBox>&nbsp;
                            <br />
                            <asp:Label runat="server" ID="DateLabel" Text="Date: " CssClass="Lable"></asp:Label>
                            <asp:TextBox runat="server" ID="DateTextBox" CssClass="TextBox" AutoCompleteType="None"> </asp:TextBox>&nbsp;
                            <Ajax:CalendarExtender ID="CalenderExtender" TargetControlID="DateTextBox" Format="dd/MM/yyyy"
                                runat="server">
                            </Ajax:CalendarExtender>
                            <br />
                            <asp:Label runat="server" ID="CustomerIdLabel" Text="Customer:" CssClass="Lable"></asp:Label>
                            <asp:TextBox runat="server" ID="InsertCustomerIdTextBox" CssClass="TextBox"> </asp:TextBox>&nbsp;
                            <br />
                        </div>
                    </div>
                    <div class="LeftAlign">
                        <div class="RirghAlign">
                            <asp:Label runat="server" ID="CommentsLabel" Text="Comments:" CssClass="Lable"></asp:Label>
                            <asp:TextBox runat="server" ID="InsertCommentsTextBox" CssClass="TextBox" TextMode="MultiLine">
                            </asp:TextBox>&nbsp;
                        </div>
                    </div>
                    <asp:GridView ID="OrderDetailsGridView" runat="server" AllowPaging="True" ViewStateMode="Disabled"
                        CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"
                        AutoGenerateColumns="False" DataKeyNames="Id" EmptyDataText="No record found for this order"
                        OnRowCommand="OrderDetailsGridView_RowCommand" PageSize="5" OnRowEditing="OrderDetailsGridView_RowEditing"
                        OnPageIndexChanging="OrderDetailsGridView_PageIndexChanging" OnRowCancelingEdit="OrderDetailsGridView_RowCancelingEdit"
                        OnRowUpdating="OrderDetailsGridView_RowUpdating" OnRowDataBound="OrderDetailsGridView_RowDataBound">
                        <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
                        <Columns>
                            <asp:TemplateField HeaderText="Prduct Id">
                                <EditItemTemplate>
                                    <asp:DropDownList ID="ProductIDDropDownList" DataValueField="Id" DataTextField="ProductName"
                                        ViewStateMode="Enabled" runat="server" Width="100%" AutoPostBack="false"  OnSelectedIndexChanged="ProductIDDropDownList_SelectedIndexChanged">
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("PrductId_FK") %>' ViewStateMode="Enabled"></asp:Label>
                                </ItemTemplate>
                                <ControlStyle Width="80px" />
                            </asp:TemplateField>
                            <asp:BoundField DataField="EmployeeId" HeaderText="Employee Id" ControlStyle-Width="100%">
                                <ControlStyle Width="30px"></ControlStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="PQuantity" HeaderText="Purchase Quantity" ControlStyle-Width="100%">
                                <ControlStyle Width="30px"></ControlStyle>
                            </asp:BoundField>
                            <asp:BoundField DataField="TotalPrice" HeaderText="Total Price" ControlStyle-Width="100%">
                                <ControlStyle Width="60px"></ControlStyle>
                            </asp:BoundField>
                            <asp:TemplateField HeaderText="Placement Date">
                                <EditItemTemplate>
                                    <asp:TextBox ID="OrderDetailsDateTimeTextBox" runat="server" Text='<%# Bind("PlacementDate") %>'></asp:TextBox>
                                    <Ajax:CalendarExtender ID="CalenderExtender" TargetControlID="OrderDetailsDateTimeTextBox"
                                        Format="dd/MM/yyyy" runat="server">
                                    </Ajax:CalendarExtender>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("PlacementDate", "{0:dd/MM/yyyy}") %>'></asp:Label>
                                </ItemTemplate>
                                <ControlStyle Width="80px" />
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Comments">
                                <EditItemTemplate>
                                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Comments") %>' TextMode="MultiLine"></asp:TextBox>
                                </EditItemTemplate>
                                <ItemTemplate>
                                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Comments") %>'></asp:Label>
                                </ItemTemplate>
                                <ControlStyle Width="150px" />
                            </asp:TemplateField>
                            <asp:CommandField ShowEditButton="True" Visible="true" ControlStyle-Width="100%"
                                CausesValidation="False">
                                <ControlStyle Width="50px"></ControlStyle>
                            </asp:CommandField>
                        </Columns>
                        <EditRowStyle />
                        <PagerStyle CssClass="pgr"></PagerStyle>
                    </asp:GridView>
                    <asp:Button ID="InserDetailsButton" runat="server" Text="New Row" CssClass="Button"
                        OnClick="InserDetailsButton_Click" />
                    <asp:Button ID="SaveChangesButton" runat="server" Text="Save Changes" CssClass="Button"
                        OnClick="SaveChangesButton_Click" />
                    <asp:Button ID="PopupCancelButton" runat="server" Text="Cancel" CssClass="Button"
                        OnClick="PopupCancelButton_Click" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </asp:Panel>

HTML





and here is .Cs code


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ObjectLayer;
using OrderPlacementBLL;
using AjaxControlToolkit;
using System.Web.Services;
namespace Order_Placement.WebForms.Orders
{
    public partial class OrdersListing : System.Web.UI.Page
    {
        List<order> OrderList;
        OrdersBLL OrderBLL;
        List<products> products;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindOrderListDataGrid();
                products = new ProductsBLL().GetAllProducts();
                ViewState["product"] = products;
            }
        }

        protected void OrderListingGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit")
            {
                BindOrderListDataGrid();
            }
        }

        protected void OrderListingGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            OrderListingGridView.PageIndex = e.NewPageIndex;
            BindOrderListDataGrid();
        }

        void BindOrderListDataGrid()
        {
            OrderList = new List<order>();
            OrderBLL = new OrdersBLL();
            OrderList = OrderBLL.GetAllOrders();
            OrderListingGridView.DataSource = OrderList;
            OrderListingGridView.DataBind();
        }
        void BindOrderDetailsDataGrid()
        {
            if (Session["order"] != null)
            {
                Order temporder = (Order)Session["order"];
                OrderDetailsGridView.DataSource = temporder.lstOrderDetails;
                OrderDetailsGridView.DataBind();

                OrderModalPopup.Show();
            }
        }
        protected void OrderListingGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            OrderModalPopup.Show();
            IdTextBox.Text = OrderListingGridView.Rows[Convert.ToInt32(e.NewEditIndex)].Cells[0].Text;
            DateTextBox.Text = OrderListingGridView.Rows[Convert.ToInt32(e.NewEditIndex)].Cells[1].Text;
            InsertCustomerIdTextBox.Text = OrderListingGridView.Rows[Convert.ToInt32(e.NewEditIndex)].Cells[2].Text;
            InsertCommentsTextBox.Text = OrderListingGridView.Rows[Convert.ToInt32(e.NewEditIndex)].Cells[3].Text;
            
            OrderBLL = new OrdersBLL();
            Order ord = new Order();
            ord.lstOrderDetails = OrderBLL.GetDetails(Convert.ToInt32(IdTextBox.Text));
            Session["order"] = ord;
            
            BindOrderDetailsDataGrid();
            
            e.Cancel = true;
        }

        protected void EditButton_Click(object sender, EventArgs e)
        {
            OrderModalPopup.Show();
        }

        protected void btn_Click(object sender, EventArgs e)
        {
            //
        }

        protected void OrderDetailsGridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            
            BindOrderDetailsDataGrid();

            if (e.CommandName.ToLower() == "update")
            {
                GridViewRow row = OrderDetailsGridView.Rows[Convert.ToInt32(e.CommandArgument)];
                DropDownList ProductIDDropDownList = row.FindControl("ProductIDDropDownList") as DropDownList;
            }

           
            
        }

        protected void SaveChangesButton_Click(object sender, EventArgs e)
        {

        }

        protected void InserDetailsButton_Click(object sender, EventArgs e)
        {
            if( Session["order"] != null )
            {
                Order temporder = (Order)Session["order"];
                temporder.lstOrderDetails.Add(new OrderDetails());
                Session["order"] = temporder;
                BindOrderDetailsDataGrid();
            }
        }

        protected void OrderDetailsGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            OrderDetailsGridView.EditIndex = e.NewEditIndex;
            GridViewRow row = OrderDetailsGridView.Rows[e.NewEditIndex];
            DropDownList ProductIDDropDownList = row.FindControl("ProductIDDropDownList") as DropDownList;

            if(ProductIDDropDownList != null)
            {
                if (ViewState["product"] != null)
                {
                    

                    ProductIDDropDownList.DataSource = ViewState["product"] as List<products>;
                    ProductIDDropDownList.DataBind();
                    ProductIDDropDownList.SelectedIndex = -1;

                    ProductIDDropDownList.SelectedIndexChanged += new EventHandler(ProductIDDropDownList_SelectedIndexChanged);

                }
            }



        }

        protected void OrderDetailsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            OrderDetailsGridView.PageIndex = e.NewPageIndex;

            BindOrderDetailsDataGrid();
        }

        protected void OrderDetailsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {

        }

        protected void OrderDetailsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            //GridView temp = (GridView)sender;
            //GridViewRow row = temp.Rows[e.RowIndex];
            //DropDownList ProductIDDropDownList = row.FindControl("ProductIDDropDownList") as DropDownList;
        }


        protected void OrderDetailsGridView_DataBinding(object sender, EventArgs e)
        {

        }

        protected void OrderDetailsGridView_DataBound(object sender, EventArgs e)
        {

        }

        protected void OrderDetailsGridView_RowCreated(object sender, GridViewRowEventArgs e)
        {

        }

        protected void OrderDetailsGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
                
        }

        protected void ProductIDDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void PopupCancelButton_Click(object sender, EventArgs e)
        {
            OrderDetailsGridView.EditIndex = -1;

            OrderModalPopup.Hide();
        }

       

        

       
       
    }
}
Posted
Updated 6-Dec-12 22:46pm
v3

check the autopostback property it should be true to fire the event
   
First change AutoPostback property to trye of dropdown
C#
<asp:dropdownlist id="ProductIDDropDownList" datavaluefield="Id" datatextfield="ProductName" viewstatemode="Enabled" runat="server" width="100%" autopostback="True" onselectedindexchanged="ProductIDDropDownList_SelectedIndexChanged" xmlns:asp="#unknown"></asp:dropdownlist>


Then add trigger to update panel as you are using update panel with conditional mode

C#
<asp:updatepanel id="OrderDetailPanel" runat="server" xmlns:asp="#unknown">
    <triggers>
        <asp:asyncpostbacktrigger controlid="OrderDetailsGridView" />
    </triggers>
 <contenttemplate>
 </contenttemplate></asp:updatepanel>
   
In UpdatePanel tag remove UpdateMode="Conditional" otherwise in dropdown selected index changed event call OrderDetailPanel.Update();.

After that set dropdown AutoPostBack="True" in your gridview
   

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

  Print Answers RSS
Top Experts
Last 24hrsThis month



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