Как привязать данные к представлению таблицы из файла XML - PullRequest
0 голосов
/ 28 октября 2018

В настоящее время у меня есть эта проблема, когда я пытаюсь привязать данные из XML-файла к представлению сетки.Я искал в сети и нашел решения для проблем, похожих на мою, но по какой-то причине они не сработали.Следующий код у меня есть

Index.aspx

 <asp:GridView ID="gvRegistrations" runat="server" AutoGenerateColumns="false">
        <Columns>
            <%-- ID --%>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_ID" runat="server" Text='<%# Bind ("id")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Full Name--%>
            <asp:TemplateField HeaderText="Full Name">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_FullName" runat="server" Text='<%# Bind ("fullName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Email--%>
            <asp:TemplateField HeaderText="Email">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_Email" runat="server" Text='<%# Bind("email") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Registration Type--%>
            <asp:TemplateField HeaderText="Registration Type">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_RegistrationType" runat="server" Text='<%# Bind ("registrationType") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Attending Social Event--%>
               <asp:TemplateField HeaderText="Attending">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_Attending" runat="server" Text='<%# Bind ("attendingSocialEvent") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>  

Registrations.xml

<?xml version="1.0" encoding="UTF-8"?>
<RegistrationCollection>
   <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>k.bernal@gmail.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>c.pierce@outlook.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>z.guy@yahoo.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>k.hawworth@bigpond.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
   </registrations>
</RegistrationCollection>

Index.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/registrations.xml"));
        gvRegistrations.DataSource = ds;
        gvRegistrations.DataBind();
    }

Это похоже на решения, которые я нашел, но проблема в том, что gvRegistrations не объявлено, но я не знаю, где и как я должен объявить это.Я знаю, что мне чего-то не хватает, просто не знаю, что ...

1 Ответ

0 голосов
/ 29 октября 2018
<?xml version="1.0" encoding="UTF-8"?>

   <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>k.bernal@gmail.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>c.pierce@outlook.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>z.guy@yahoo.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>k.hawworth@bigpond.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
   </registrations>

Благодаря @VDWWD, после внесенных здесь изменений он заработал

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...