Click here to Skip to main content
15,888,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to find the control which has generated the event and based on that found control I have to Perform other process.

In my page there is 2 dropdown list, other text boxes and one gridview.

I have only neccessary code here.

Following is my code:

Aspx page Code:
ASP.NET
<%@ Page Title="" Language="C#" MasterPageFile="~/Admin/Admin.master" AutoEventWireup="true"
        CodeFile="Overview.aspx.cs" Inherits="Admin_Overview" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
    
    First Dropdown :
    <asp:DropDownList ID="drpClass" runat="server" AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="drpClass_SelectedIndexChanged">
    
    
    Second Dropdown :
    <asp:DropDownList ID="drplstFran" runat="server" AppendDataBoundItems="True" AutoPostBack="True" OnSelectedIndexChanged="drplstFran_SelectedIndexChanged">
    
    
    <asp:GridView ID="GridView1" runat="server" DataKeyNames="nOverviewId" ForeColor="#333333"
                                            AllowPaging="True" AllowSorting="True" PageSize="12" AutoGenerateColumns="False"
                                            CellSpacing="2" CellPadding="4" GridLines="Horizontal" CssClass="clsGrid" BorderStyle="Solid"
                                            BorderColor="#008BDF" BorderWidth="2px" Style="margin-left: 100px;" OnPageIndexChanging="grd_PageIndexChanging"
                                            OnRowDeleting="grd_RowDeleting" OnSelectedIndexChanging="grd_SelectedIndexChanging"
                                            OnSorting="grd_Sorting" PagerSettings-PageButtonCount="10" OnSelectedIndexChanged="grd_SelectedIndexChanged">
                                            <alternatingrowstyle backcolor="#D7EBFF" bordercolor="#003366" borderstyle="Solid">
                                                BorderWidth="1px" CssClass="GrdAltWorStl" />
                                            <columns>
                                                <asp:TemplateField>
                                                    <HeaderTemplate>
                                                        <table width="100%" cellpadding="5" cellspacing="1">
                                                            <tr>
                                                                <td align="left" style="width: 100px">
                                                                    <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" CommandName="sort"
                                                                        CommandArgument="dtMonthYear">Month-Year
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </HeaderTemplate>
                                                    <itemtemplate>
                                                        <asp:Panel ID="PopupMenu" runat="server">
                                                            <div style="padding-removed 2px;">
                                                                <asp:ImageButton ID="ModifyLnk" runat="server" CommandName="Select" EnableTheming="false"
                                                                    ImageUrl="~/Admin/Images/edit.png" CausesValidation="false" />
                                                                <asp:ImageButton ID="DelLnk" runat="server" ImageUrl="~/Admin/Images/delete.png"
                                                                    CommandName="Delete" EnableTheming="false" CausesValidation="false" OnClientClick="return DelOverview();" />
                                                            </div>
                                                        
                                                        <asp:Panel ID="Panel9" runat="server">
                                                            <table width="100%" cellpadding="0" cellspacing="1">
                                                                <tr>
                                                                    <td align="left" style="width: 100px">
                                                                        <asp:Label ID="lblGrdMonthYear" runat="server" Text='<%# Eval("dtMonthYear")%>'>
                                                                    </td>
                                                                </tr>
                                                            </table>
                                                        
                                                        <cc1:HoverMenuExtender ID="hme2"  runat="server" HoverCssClass="popupHover" PopupControlID="PopupMenu"
                                                            PopupPosition="Left" TargetControlID="Panel9" PopDelay="25">
                                                        
                                                    </itemtemplate>
                                                
                                            </columns>
                                            <editrowstyle backcolor="#80FFFF" cssclass="GrdEdtRowStl" />
                                            <footerstyle backcolor="#507CD1" font-bold="True" forecolor="White" cssclass="GrdHdr" />
                                            <HeaderStyle BackColor="#2FB1FF" Font-Bold="True" ForeColor="White" BorderStyle="Solid"
                                                BorderWidth="1.5px" CssClass="GrdHdr" BorderColor="#008BDF" />
                                            <pagersettings firstpageimageurl="Images/Prevoius.png" lastpageimageurl="Images/Next.png">
                                                Mode="NumericFirstLast" PageButtonCount="5" />
                                            <pagerstyle backcolor="#2461BF" forecolor="White" horizontalalign="Center" cssclass="gridview1" />
                                            <rowstyle backcolor="#EFF3FB" cssclass="GrdRowStyle" />
                                            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                                            <sortedascendingcellstyle backcolor="#F5F7FB" />
                                            <sortedascendingheaderstyle backcolor="#6D95E1" />
                                            <sorteddescendingcellstyle backcolor="#E9EBEF" />
                                            <sorteddescendingheaderstyle backcolor="#4870BE" />


Note: I have used Ajax control toolkit.

Code Behind :

C#
protected void Page_Load(object sender, EventArgs e)
    {
     if (Page.IsPostBack)
     {
        if (getPostBackControlName() == "drpClass")
        {
          code here.....
        }
        else if (getPostBackControlName() == "drplstFran")
        {
           code here.....
        }
        else if (getPostBackControlName() == "ModifyLnk")
        {
          code here.....
        }
     }
    }
    private string getPostBackControlName()
        {
            Control control = null;
           
            string ctrlname = Page.Request.Params["__EVENTTARGET"];
            if (ctrlname != null && ctrlname != String.Empty)
            {
                control = Page.FindControl(ctrlname);
            }
           else
            {
                string ctrlStr = String.Empty;
                Control c = null;
                
                foreach (string ctl in Page.Request.Form)
                {
                    if (ctl.EndsWith(".x") || ctl.EndsWith(".y"))
                    {
                        ctrlStr = ctl.Substring(0, ctl.Length - 2);
                        c = Page.FindControl(ctrlStr);
                    }
                    else
                    {
                        c = Page.FindControl(ctl);
                    }
                    if (c is System.Web.UI.WebControls.DropDownList)
                    {
                            control = c;
                            break;
                    }
                }
            }
            return control.ID;
    
        }




I found correct control name when I select dropdown list. but when i use imagebutton of grid view following error message populate.
HTML
"Invalid postback or callback argument.  Event validation is enabled using <pages enableeventvalidation="true" /> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation."


How to resolve this error.
Posted
v9
Comments
First of all, please accept the answer- How can I know which event is fired?[^]

Now for this error, try to debug and see on which line it is throwing the exception.

If you are refering to Get control name in Page_Load event which make the post back[^], then the function is different here.

The if block inside the loop is...
C#
if (c is System.Web.UI.WebControls.Button ||
        c is System.Web.UI.WebControls.ImageButton)
{
   control = c;
   break;
}
 
Share this answer
 
Refer the link below

http://www.nullskull.com/articles/20050609.asp[^]

Should give you some insight on the same.
 
Share this answer
 

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