Prevent selectedindexchange to be fired

Posted on 2013-09-26
Medium Priority
Last Modified: 2013-10-03

I have a gridview that:
- moves to another page when SelectedIndexChanged event is fired when a row is selected
- Has a delete button, with a confirm button extender, to delete the row

So When I click on the delete button, the message to delete the row is fired, then the SelectedIndexChanged event is fired, so the DeleteRowCommand never happens.

How can I prevent the SelectedIndexChanged from being fired?

Thank you for your help

Code below:

In the ascx control:
<asp:GridView ID="GridView1" runat="server" DataSourceID="DS" DataKeyNames="JobID" 
OnRowDataBound="GV_RowDataBound" OnSelectedIndexChanged="GV_SelectedIndexChanged" OnDataBound="GV_DataBound" 
Caption="Bundled jobs">
        <asp:TemplateField HeaderText="Job number">
                <asp:Label ID="lblJobRefNo" runat="server" Text='<%# Eval("JobRefNo") %>' Visible="True" Font-Bold='<%# Eval("IsBold") %>' />
        <asp:TemplateField HeaderText="Remove Job">
                <asp:ImageButton ID="btnDelete" runat="server" AlternateText="Del"
                ImageUrl="~/App_Themes/Default/Images/Buttons/btnDelete.gif" ImageAlign="middle" 
                CommandName="Delete" CommandArgument='<%# Eval("JobID") %>' OnCommand="DeleteJob_RowCommand" Visible='<%# Eval("IsEnabled") %>' />
                <asp:ConfirmButtonExtender ID="cnfmBtnExt" runat="server" TargetControlID="btnDelete" 
                ConfirmText="Are you sure you want to remove this job from the bundle?"/>

Open in new window

In the ascx.cs code:
    protected void GV_SelectedIndexChanged(object sender, EventArgs e)

        if (this.LJobID != int.Parse(GV.SelectedDataKey[0].ToString()))
            this.LJobID = int.Parse(GV.SelectedDataKey[0].ToString());

        EventHandler handler = SelectJob;
                if (SelectJob!=null)
                    handler(sender, e);

    protected void DeleteJob_RowCommand(object sender, System.Web.UI.WebControls.CommandEventArgs e)

        GridViewRow GVRow = (GridViewRow)((ImageButton)sender).NamingContainer;
        int JobID = int.Parse(((HiddenField)GVRow.FindControl("hfJobID")).Value);

        DTO.AssocBundle d = new DTO.AssocBundle();
        BLL.AssocBundle DL = new BLL.AssocBundle();
        d.JobID = JobID;
        d.LJobID = LJobID;
        d.UpdatedBy = NVar.UserName;
        d.UpdType = "Delete";


Open in new window

Question by:AnneF
  • 4
  • 3
LVL 13

Expert Comment

by:Rahul Agarwal
ID: 39526956
I think there is no way to avoid it

e.Row.Cells(i).Attributes("ondblclick") = Page.ClientScript.GetPostBackClientHyperhttp://www.aspforums.net/this.gvTask, "Select$" + e.Row.RowIndex;

change ( to [ and ) to ]

LVL 41

Expert Comment

ID: 39529052
I just added your deleted column code to one of a sample GV. I see that DeleteRowCommand was fired and then RowCommand was fired but not SelectedIndexChanged.

Is there something else in your code-behind we are missing?

Author Comment

ID: 39529158
Yes, there is a bit of code missing. The control is in a form,and the SelectedIndexChanged is fired from this form.

Code below:
In the aspx form:
  <pts:AssociatedBundle ID="ctlAssociatedBundle" runat="server" OnSelectJob="AssocBundle_Select"/>   

Open in new window

In the aspx.cs Code:
    protected void AssocBundle_Select(object sender, EventArgs e) 
        _JobID = this.ctlAssociatedBundle.LJobID;
        _IsAimpd = this.ctlJob.IsAimpd;


Open in new window

SetupQS - Set or retrieve the parameters of the query string and redirect to a page with new parameters:
   protected void SetupQS(string status)
        UrlManager url = new UrlManager();

        switch (status)
            case "Set":
                using (QSH QH =new QSH())
                    QH.UrlType = _UrlType;
                    QH.UrlGroup = _urlGroup;
                    QH.BaseUrl = url.GetUrl(_UrlType);
                    QH.MenuIndex = _MenuIndex.ToString();
                    QH.MenuName = _MenuName;
                    QH.OpID = _EmpID.ToString();
                    QH.OpDesc = _EmpName;
                    QH.EmpType = _EmpType;
                    QH.DocID = _DocID.ToString();
                    QH.JobID = _JobID.ToString();
                    QH.IsAimpd = _IsAimpd.ToString();

            case "Retrieve":
                using (QSH QH =new QSH(Request.QueryString))
                        _urlGroup = QH.UrlGroup;
                        _UrlType = QH.UrlType;
                        _MenuIndex = int.Parse(QH.MenuIndex);
                        _MenuName = QH.MenuName;
                        _EmpID = int.Parse(QH.OpID);
                        _EmpName = QH.OpDesc;
                        _EmpType = QH.EmpType;
                        _DocID = int.Parse(QH.DocID);
                        _JobID = int.Parse(QH.JobID);
                        _IsAimpd = int.Parse(QH.IsAimpd);

Open in new window

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 41

Expert Comment

ID: 39529177
From what I am understanding, GV SelectedIndexChanged and SetupQS will not fired unless Select Command from GV is fired.
Are you calling SetupQS  from somewhere else?

Author Comment

ID: 39530436
Yes, you are right. GV SelectedIndexChanged and SetupQS are only fired from the GV Select Command.
SetupQS is also called from  2 other similar gridviews in the same form.
And called as well in the Page_Load event, but to retrieve info from the Query string, SetupQS("Retrieve")
LVL 41

Accepted Solution

guru_sami earned 2000 total points
ID: 39530438
Can you share your page load?
Make sure setupqs is not called on postback.

Assisted Solution

AnneF earned 0 total points
ID: 39530468
I found the solution. Thank you for your indirect help. I checked if SetupQS was called on postback and found that the problem was a validation to add a record that was not disabled to delete the record, therefore creating an error.
Thank you for your time, and questions asked.

Author Closing Comment

ID: 39542519
As I was following Guru_Sami suggestion, I found that the problem was somewhere else in the code.

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
An ASP.NET Web Form User Control is not newly introduced in ASP.NET. In fact, it was an old technology yet still playing a role to generate web content, especially when we want to use it to have a better and easy way to control part of the web conte…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …

588 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question