无法找到产生事件的正确控制 [英] Unable to found right control which has generated the event

查看:66
本文介绍了无法找到产生事件的正确控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须找到生成事件的控件,并根据找到的控件我必须执行其他过程。



在我的页面中有2个下拉列表列表,其他文本框和一个gridview。



我这里只有必要的代码。



以下是我的代码:



Aspx页面代码:

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:

<%@ 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 :





Note: I have used Ajax control toolkit.

Code Behind :

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.





I found correct control name when I select dropdown list. but when i use imagebutton of grid view following error message populate.

"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.



How to resolve this error.

推荐答案

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...

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...
if (c is System.Web.UI.WebControls.Button ||
        c is System.Web.UI.WebControls.ImageButton)
{
   control = c;
   break;
}


Refer the link below



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



Should give you some insight on the same.
Refer the link below

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

Should give you some insight on the same.


这篇关于无法找到产生事件的正确控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆