1

削除ボタンのあるネストされたリピーターがあります。このボタンは、グループから生徒を削除します。しかし、削除ボタンを押すと、ネストされたリピーターを再び通過し、次のようになります。

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 には、グループ 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 には値がありません。

4

1 に答える 1

2

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); 
} 
于 2012-05-21T19:11:12.327 に答える