【问题标题】:Imagebutton delete action in nested control嵌套控件中的图像按钮删除操作
【发布时间】:2012-05-21 19:01:30
【问题描述】:

我有一个嵌套的中继器,里面有一个删除按钮。此按钮从组中删除学生。但是当我按下删除按钮时,它再次通过嵌套中继器,我得到:

Invalid postback or callback argument.

堆栈跟踪:

[ArgumentException: 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.]
System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument) +9714590
System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument) +111
System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +29
  System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

我的 asp.net 文件:

<!-- Begin modal -->
<asp:Repeater ID="RepeaterModal" OnItemDataBound="RepeaterModal_ItemDataBound" runat="server">
    <HeaderTemplate>
    <div id="modal">
    </HeaderTemplate>
    <ItemTemplate> 
        <div id="dialog<asp:Literal ID='ltlModalNumber' runat='server' />" class="window">
            <div class="contents">
                <h3>Students in group <asp:Literal ID="ltlModalGroup" runat="server" /></h3>
                <ul>
                <asp:Repeater ID="repeaterModalStudentList" Runat="server">
                    <ItemTemplate>
                        <li class="modalStudent"><%# Eval("Name") %></li>
                        <li class="modalStudentClassDelete">
                            <asp:ImageButton ID="imgDeleteStudent" runat="server" ImageUrl="styles/img/icons/2.png" CommandName="deleteStudent" OnClick="btnDeleteStudent_Click" ToolTip="Delete this student" />
                        </li>
                    </ItemTemplate>
                </asp:Repeater>
                </ul>                       

                <a href="#" class="close">Close</a>
            </div>
        </div>
    </ItemTemplate> 
    <FooterTemplate>
    </div>
    </FooterTemplate>

</asp:Repeater>
<!-- End modal -->

在我的文件后面的代码中:

//A field 
int r = 0;

//Populates the table with the list of groups.
RepeaterModal.DataSource = listOfGroups;
RepeaterModal.DataBind();

listOfGroups 包含一个包含 Group 对象的列表,其中包含 group_Id、名称、代码、学生对象以及学生姓名的字符串。

//Repeater methode to put the values in the correct labels of the modal window
public void RepeaterModal_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    //Execute the following logic for Items and Alternating Items.
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        ((Literal)e.Item.FindControl("ltlModalNumber")).Text = ((Groups)e.Item.DataItem).Group_Id.ToString();
        ((Literal)e.Item.FindControl("ltlModalGroup")).Text = ((Groups)e.Item.DataItem).Code.ToString();    

        //Fill the repeater inside the repeater with the students name
        Repeater repeaterModalStudentList = ((Repeater)e.Item.FindControl("repeaterModalStudentList"));
        repeaterModalStudentList.DataSource = ((Groups)e.Item.DataItem).Students;
        repeaterModalStudentList.DataBind();

        ImageButton imgDeleteStudent = repeaterModalStudentList.Items[0].FindControl("imgDeleteStudent") as ImageButton;

        if (imgDeleteStudent != null) {
                imgDeleteStudent.CommandArgument = ((Groups)e.Item.DataItem).Students[r].Student_Id.ToString();
                r++;
        }
    }
}

protected void btnDeleteStudent_Click(object sender, EventArgs e) {     
    ImageButton b = (ImageButton)sender;
    string value = b.CommandArgument;

    Students student = new Students();
    student.DeleteStudent(int.Parse(value));

    Response.Redirect(Request.RawUrl);
}

我错过了什么或做错了什么,我不断收到这个错误?添加 EnableEventValidation 不是解决方案。这是 commandArgument 的东西。

编辑

大声笑,添加了 if(!IsPostBack),不再出现错误。但是 CommandArgument 中没有值。

【问题讨论】:

  • 组 id 是否有任何时髦的字符?通过时髦,我指的是任何会触发回发验证的角色。
  • 不,它只是一个 int,例如1、2、9 等等。我在 On_Load 方法中忘记了 if(!IsPostBack)。这就是它重复它的原因。但仍然报错,imgDeleteStudent 只找到了两次,而应该找到了 5 次。
  • 更正,我不会再收到错误。但还是不行。添加到命令参数的值为空。

标签: c# asp.net nested-repeater


【解决方案1】:

您正在分配 CommandArgument,但您的方法是 btnDeleteStudent_Click

更新:

//Repeater methode to put the values in the correct labels of the modal window 
public void RepeaterModal_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { 
    //Execute the following logic for Items and Alternating Items. 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { 
        ((Literal)e.Item.FindControl("ltlModalNumber")).Text = ((Groups)e.Item.DataItem).Group_Id.ToString(); 
        ((Literal)e.Item.FindControl("ltlModalGroup")).Text = ((Groups)e.Item.DataItem).Code.ToString();     

        //Fill the repeater inside the repeater with the students name 
        Repeater repeaterModalStudentList = ((Repeater)e.Item.FindControl("repeaterModalStudentList")); 
        repeaterModalStudentList.DataSource = ((Groups)e.Item.DataItem).Students; 
        repeaterModalStudentList.DataBind(); 
        repeaterModalStudentList.ItemDataBound += repeaterModalStudentList_ItemDataBound; 
    } 
} 

//Repeater methode to put the values in the correct labels of the modal window 
public void repeaterModalStudentList_ItemDataBound(Object Sender, RepeaterItemEventArgs e) { 
    //Execute the following logic for Items and Alternating Items. 
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {       

        ImageButton imgDeleteStudent = repeaterModalStudentList.Items[0].FindControl("imgDeleteStudent") as ImageButton; 

        if (imgDeleteStudent != null) { 
            imgDeleteStudent.CommandArgument = ((Student)e.Item.DataItem).Student_Id.ToString(); 
        } 
    } 
} 


protected void btnDeleteStudent_Click(object sender, EventArgs e) {  
    ImageButton btn = (ImageButton)sender; 
    int studentId = (int)btn.CommandArgument; 

    Students student = new Students(); 
    student.DeleteStudent(studentId); 

    Response.Redirect(Request.RawUrl); 
} 

【讨论】:

  • 我想你的意思是他应该订阅ItemCommand,而不是btnDeleteStudent_Click?
  • 你是对的,但这只是一个命名约定,因为我的属性 OnClick 具有相同的名称。
  • 我更新了代码。您可以从 aspx 页面或后面的代码(如上面的代码)附加到 btnDeleteStudent_Command。
  • 必须是CommandEventArgs;否则,参数将不匹配。
  • @Win 是的,你是对的!我多么愚蠢,我应该意识到这一点,但我当然没有意识到,因为我在网上找到了有关嵌套中继器的信息。像这个:codeproject.com/Articles/6140/… 但是这只是谈论数据绑定一个数据源而不是如何使用 onItemDataBound 添​​加自己的额外代码。谢谢,我的朋友,这是您接受的答案。
猜你喜欢
  • 2011-10-12
  • 1970-01-01
  • 2013-02-13
  • 1970-01-01
  • 2016-03-02
  • 1970-01-01
  • 2023-03-09
  • 2021-05-29
  • 2015-11-05
相关资源
最近更新 更多