开发者

Save checkboxlist values to xml using vb.net

I have some checkboxlist like this:

    <asp:CheckBoxList ID="G1" runat="server">
                        <asp:ListItem Value="Comunicações Unificadas" Text="Comunicações Unificadas - UCoIP"></asp:ListItem>
                        <asp:ListItem Value="Gestão Documental" Text="Gestão Documental - iPortalDoc"></asp:ListItem>
                        <asp:ListItem Value="Gestão Conteúdos Web" Text="Gestão de Conteúdos Web"></asp:ListItem>
                        <asp:ListItem Value="Promoção Websites" Text="Promoção de Websites"></asp:ListItem>
                        <asp:ListItem Value="Serviços de Consultoria" Text="Serviços de Consultoria"></asp:ListItem>
</asp:CheckBoxList>

When submitting the form than contains them, i want to save all the selected values in a xml file.

At the moment if i select more than one value to submit, in the xml file i only get the first selected, the others do not appear in the file.

I'm saving the values in the xml file this way:

Dim doc As New XmlDocument() doc.Load(LocalizacaoFichei开发者_运维百科ro)

    Dim visitor As XmlElement = doc.CreateElement("Cliente")

    Dim res1 As XmlElement = doc.CreateElement("Resposta1")
    res1.InnerText = G1.SelectedValue.ToString


    visitor.AppendChild(res1)

    doc.DocumentElement.AppendChild(visitor)

    doc.Save(LocalizacaoFicheiro)

I need to change something in the way that i save the data to xml, right?


CheckBoxList.SelectedValue is just a single value, so of course you're only getting one value when you use that property. From its definition it's designed to give you only the first selected value:

If multiple items are selected, the value of the selected item with the lowest index is returned.

Use CheckBoxList.Items instead, looping through all of them and checking the ListItem.Selected property to see if they've been selected or not. (Or use the equivalent LINQ expression, whichever you prefer.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜