ASPX ו- C# שגיאת קומפיילר
שלום, אני לומד C# מהספר של WROX. יש שם קוד שניסיתי לשכלל וקיבלתי שגיאת קומפילציה. אולי מישהו יכל להסביר לי למה קיבלתי את השגיאה ואיך צריך לכתוב את הקוד כדי להמנע משגיאה זו בעתיד. (אני מתנצל מראש על כמות הטקסט הגדולה) להלן הקוד שעובד ללא בעייה (דוגמא מהספר):
זו הודעת השגיא מהקומפיילר:
בתודה מראש, בוריס.
שלום, אני לומד C# מהספר של WROX. יש שם קוד שניסיתי לשכלל וקיבלתי שגיאת קומפילציה. אולי מישהו יכל להסביר לי למה קיבלתי את השגיאה ואיך צריך לכתוב את הקוד כדי להמנע משגיאה זו בעתיד. (אני מתנצל מראש על כמות הטקסט הגדולה) להלן הקוד שעובד ללא בעייה (דוגמא מהספר):
<script runat="server" language="C#"> void Page_Load(){ if (Page.IsPostBack){ string msg = "You have selected:<br/>"; if (list1.Items[0].Selected) msg = msg + list1.Items[0].Text + "<br/>"; if (list1.Items[1].Selected) msg = msg + list1.Items[1].Text + "<br/>"; if (list1.Items[2].Selected) msg = msg + list1.Items[2].Text + "<br/>"; if (list1.Items[3].Selected) msg = msg + list1.Items[3].Text + "<br/>"; if (list1.Items[4].Selected) msg = msg + list1.Items[4].Text + "<br/>"; if (list1.Items[5].Selected) msg = msg + list1.Items[5].Text + "<br/>"; if (list1.Items[6].Selected) msg = msg + list1.Items[6].Text + "<br/>"; Message.Text = msg; } } </script> <html> <head> <title>List Box example</title> </head> <body> <asp:label id="Message" runat="server" /> <br/> <form runat="server"> Which city do you wish to look at hotels for?<br/><br/> <asp:listbox id="list1" runat="server" selectionmode="Multiple"> <asp:listitem>Madrid</asp:listitem> <asp:listitem>Oslo</asp:listitem> <asp:listitem>Lisbon</asp:listitem> <asp:listitem>Paris</asp:listitem> <asp:listitem>London</asp:listitem> <asp:listitem>Berlin</asp:listitem> <asp:listitem>Jerusalem</asp:listitem> </asp:listbox> <br/><br/> <input type="submit" /> </form> </body> </html>
...וזה הניסיון שלי בלשכלל את הקוד:void Page_Load(){ if (Page.IsPostBack){ string msg = "You have selected:<br/>"; for (int i=0; i <= 6; i++){ if (list1.items.Selected) msg = msg + list1.Items.Text + "<br/>"; } Message.Text = msg; } }
זו הודעת השגיא מהקומפיילר:
CS0122: 'System.Web.UI.WebControls.ListControl.items' is inaccessible due to its protection level Line 5: Line 6: for (int i=0; i <= 6; i++){ Line 7: if (list1.items.Selected){ Line 8: msg = msg + list1.Items.Text + "<br/>"; Line 9: }
בתודה מראש, בוריס.