如何使用Silverlight / C#删除HtmlElement中的所有子项 [英] How to remove all children in HtmlElement with Silverlight/C#

查看:138
本文介绍了如何使用Silverlight / C#删除HtmlElement中的所有子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个Silverlight应用程序通过 HTML表单提交调用一些报告(.rdlc)。
这个表单是通用的,所以我用它来调用所有的报表。



现在我想清除表单中的输入元素submited。



因此,每次我想用无子调用新报告时,我都需要清除表单

任何人都可以解释为什么当表单中有超过5个(或更大数量的输入)输入时,这不起作用吗?

  public static void RemoveInputsFromForm(HtmlElement Form)
{
if(HtmlPage.Document.GetElementById(Form.Id)!= null)
{
foreach(Form.Children中的HtmlElement元素)
{
if(element.Id!= string.Empty)
{
element.Parent .RemoveChild(元件);
}
}
}
}

表单有它的ID,每个输入都有一个ID,它随机删除一些输入,其他停留在表单中。

<

  public static void AddInputToForm(HtmlElement Form,string Name, (Data!= null&& Data.ToString()!= string.Empty)
{
var input = HtmlPage.Document.CreateElement ( 输入);
input.SetProperty(type,hidden);
input.SetProperty(value,Data.ToString());
input.SetProperty(name,Name);
input.SetProperty(id,Name);
Form.AppendChild(input);




$ b

这是我现在用来移除表单的方法子元素,它与 AddInputToForm 方法相同,仅用于删除输入

  public static void RemoveInputFromForm(HtmlElement Form,string Name)
{
$ b $ if(HtmlPage.Document.GetElementById(Name)!= null)
{
Form。 removeChild之(HtmlPage.Document.GetElementById(名称));
}

}

这是让我的方法form

  public static HtmlElement GetForm(string formUrl)
{
//在html中包含silverlight对象,则必须具有名为reportform的表单
//< form id =reportformaction =method =posttarget =_ blankstyle =visibility:collapse/>
HtmlElement functionReturnValue = null;


//设置表单
functionReturnValue = HtmlPage.Document.GetElementById(reportform);

// set form action
functionReturnValue.SetProperty(action,formUrl);


return functionReturnValue;

$ / code>

重现代码(SomeElement是一个带有字符串属性的对象): p>

  //获取表单
var Form = GetForm(ApplicationUrl + @Reports\Report.aspx);

//添加参数以形成
AddInputToForm(Form,id1,SomeElement.Id1Value);
AddInputToForm(Form,id2,SomeElement.Id2Value);
AddInputToForm(Form,id3,SomeElement.Id3Value);
AddInputToForm(Form,id4,SomeElement.Id4Value);
AddInputToForm(Form,id5,SomeElement.Id5Value);
AddInputToForm(Form,id6,SomeElement.Id6Value);
AddInputToForm(Form,id7,SomeElement.Id7Value);
AddInputToForm(Form,id8,SomeElement.Id8Value);
AddInputToForm(Form,id9,SomeElement.Id9Value);

//提交表单
Form.Invoke(submit);

//清理报告
RemoveInputFromForm(Form,id1);
RemoveInputFromForm(Form,id2);
RemoveInputFromForm(Form,id3);
RemoveInputFromForm(Form,id4);
RemoveInputFromForm(Form,id5);
RemoveInputFromForm(Form,id6);
RemoveInputFromForm(Form,id7);
RemoveInputFromForm(Form,id8);
RemoveInputFromForm(Form,id9);

所以你看我必须删除我添加的每个孩子,表单,我希望只有一次调用并清除该表单



提前感谢任何给定的解决方案。

解决方案

foreach中不应添加或移除元素循环,因为集合更改并使循环无效。而是将所有要删除的项目添加到单独的列表中,然后在第二个循环中删除它们。 (我没有测试过,因为我没有相关的示例代码,但这是 foreach 循环中的常见错误。)

 列表< HtmlElement> toRemove = new List< HtmlElement>(); 
foreach(Form.Children中的HtmlElement元素)
{
if(element.Id!= string.Empty)
{
toRemove.Add(element);
}
}
foreach(toRemove中的HtmlElement元素)
{
Form.RemoveChild(element);
}

另外,使用第二种方法,您可以将所有ID添加到列表,然后添加循环这些元素的 RemoveAllFormInputs()方法。


So I have this Silverlight application that calls some reports(.rdlc) via HTML form submit. This form is universal, so I use it to call all reports with it.

Now I want to clear the input elements in it everytime the form has been submited.

So I want a clear form everytime I want to call a new report with no children in it.

Can anyone explain why this is not working when there are more than 5(or any larger amount of inputs) inputs in the form?

    public static void RemoveInputsFromForm(HtmlElement Form)
    {
        if (HtmlPage.Document.GetElementById(Form.Id) != null)
        {
            foreach (HtmlElement element in Form.Children)
            {
                if (element.Id != string.Empty)
                {
                    element.Parent.RemoveChild(element);
                }
            }
        }
    }

The form has it's ID and every input has an ID, it removes some inputs randomly, others stay in the form.

This is the method that inserts the input elements into that form:

public static void AddInputToForm(HtmlElement Form, string Name, object Data)
    {
        if (Data != null && Data.ToString() != string.Empty)
        {
            var input = HtmlPage.Document.CreateElement("input");
            input.SetProperty("type", "hidden");
            input.SetProperty("value", Data.ToString());
            input.SetProperty("name", Name);
            input.SetProperty("id", Name);
            Form.AppendChild(input);
        }
    }

This is the method that I use now to remove form children, it is the same as AddInputToForm method only that it removes inputs

 public static void RemoveInputFromForm(HtmlElement Form, string Name)
    {

        if (HtmlPage.Document.GetElementById(Name) != null)
        {
            Form.RemoveChild(HtmlPage.Document.GetElementById(Name));
        }

    }

This is the method that gets my form

  public static HtmlElement GetForm(string formUrl)
    {
        //in the html that holds the silverlight object, you must have a form named "reportform"
        //<form id="reportform" action="" method="post" target="_blank" style="visibility: collapse" />
        HtmlElement functionReturnValue = null;


        //set form
        functionReturnValue = HtmlPage.Document.GetElementById("reportform");

        //set form action
        functionReturnValue.SetProperty("action", formUrl);


        return functionReturnValue;
    }

To reproduce the code (SomeElement is an object with string properties):

           //get form
            var Form = GetForm(ApplicationUrl + @"Reports\Report.aspx");

            //add parameters to form
            AddInputToForm(Form, "id1", SomeElement.Id1Value);
            AddInputToForm(Form, "id2", SomeElement.Id2Value);
            AddInputToForm(Form, "id3", SomeElement.Id3Value);
            AddInputToForm(Form, "id4", SomeElement.Id4Value);
            AddInputToForm(Form, "id5", SomeElement.Id5Value);
            AddInputToForm(Form, "id6", SomeElement.Id6Value);
            AddInputToForm(Form, "id7", SomeElement.Id7Value);
            AddInputToForm(Form, "id8", SomeElement.Id8Value);
            AddInputToForm(Form, "id9", SomeElement.Id9Value);

            //submit form
            Form.Invoke("submit");

            //clean report
            RemoveInputFromForm(Form, "id1");
            RemoveInputFromForm(Form, "id2");
            RemoveInputFromForm(Form, "id3");
            RemoveInputFromForm(Form, "id4");
            RemoveInputFromForm(Form, "id5");
            RemoveInputFromForm(Form, "id6");
            RemoveInputFromForm(Form, "id7");
            RemoveInputFromForm(Form, "id8");
            RemoveInputFromForm(Form, "id9");

So you see I have to remove every child I added with as many calls as there are inputs in the form, I would like to have only one call and clean that form.

Thanks in advance to any given solution.

解决方案

You should never add or remove elements while inside a foreach loop, as the collection changes and invalidates the loop. Rather add all items to be removed in a separate list and then remove them in a second loop. (I haven't tested it because I do not have relevant sample code, but this is a common mistake with foreach loops.)

List<HtmlElement> toRemove = new List<HtmlElement>();
foreach (HtmlElement element in Form.Children)
{
    if (element.Id != string.Empty)
    {
        toRemove.Add(element);
    }
}
foreach (HtmlElement element in toRemove)
{
    Form.RemoveChild(element);
}

Alternatively, using your second approach, you can always add all your IDs to a list, and then add a RemoveAllFormInputs() method which loops over these elements.

这篇关于如何使用Silverlight / C#删除HtmlElement中的所有子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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