我后面有这段代码:
protected void Page_Load(object sender, EventArgs e)
{
DdlAddPhoto.DataSource = Enumerable.Range(1, 9);
DdlAddPhoto.DataBind();
}
protected void DdlAddPhoto_SelectedIndexChanged(object sender, EventArgs e)
{
string SelectedAlbum = DdlAddPhoto.SelectedValue.ToString();
for (int i = 1; i < 20; i++)
{
string image = String.Format("Image{0}", Convert.ToString(i));
string path = String.Format("~/images/prettyPhoto/{0}/fullscreen/{1}.jpg", SelectedAlbum, Convert.ToString(i));
Image im = (Image)FindControl(image);
im.ImageUrl = path.ToString();
}
}
这个标记:
<asp:DropDownList runat="server" ID="DdlAddPhoto" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DdlAddPhoto_SelectedIndexChanged" >
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ALBUM" Value="0" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="InsertPhotoValidate" runat="server" ValidationGroup="UPDATE" ErrorMessage="Παρακαλώ Επιλέξτε Album"
ControlToValidate="DdlAddPhoto" Text="*" InitialValue="0" ForeColor="Red"> </asp:RequiredFieldValidator>
<table>
<tr><td>1</td><td><asp:Image ID="Image1" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>2</td><td><asp:Image ID="Image2" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>3</td><td><asp:Image ID="Image3" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>4</td><td><asp:Image ID="Image4" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>5</td><td><asp:Image ID="Image5" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>6</td><td><asp:Image ID="Image6" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>7</td><td><asp:Image ID="Image7" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>8</td><td><asp:Image ID="Image8" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>9</td><td><asp:Image ID="Image9" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>10</td><td><asp:Image ID="Image10" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>11</td><td><asp:Image ID="Image11" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>12</td><td><asp:Image ID="Image12" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>13</td><td><asp:Image ID="Image13" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>14</td><td><asp:Image ID="Image14" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>15</td><td><asp:Image ID="Image15" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>16</td><td><asp:Image ID="Image16" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>17</td><td><asp:Image ID="Image17" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>18</td><td><asp:Image ID="Image18" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>19</td><td><asp:Image ID="Image19" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>20</td><td><asp:Image ID="Image20" runat="server" AlternateText="N/A IMAGE" Width="100px" Height="100px" /></td><td></td></tr>
<tr><td>
我想使用 for 循环将图像 url 应用于所有 20 张图像。下拉列表为我提供了正确的专辑路径。它给了我错误:Object reference not set to an instance of an object.
在线:
> Line 25: Image im = (Image)FindControl(image);
为什么查找控件不起作用?在断点处给出 im=null