使用for循环在多个图像中应用imageurl

时间:2013-12-19 22:34:52

标签: c# asp.net for-loop drop-down-menu imageurl

我有这个代码:

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循环将图像网址应用于所有20个图像。下拉列表为我提供了正确的相册路径。它给了我错误:Object reference not set to an instance of an object.在线:

     > Line 25: Image im = (Image)FindControl(image);

为什么find控件不起作用?在断点中给出im = null

1 个答案:

答案 0 :(得分:1)

答案在这里:Find a image in code behind

我用这个:

((Image)FindControlRecursive<Image>(this,
    String.Format("Image{0}", Convert.ToString(i)))).ImageUrl = 
        String.Format("~/images/prettyPhoto/{0}/fullscreen/{1}.jpg",
            SelectedAlbum.ToString(), Convert.ToString(i));

并且也使用Antonio Bakula给出的这种方法:

public static T FindControlRecursive<T>(Control holder, string controlID) where T : Control
{
    Control foundControl = null;
    foreach (Control ctrl in holder.Controls)
    {
        if (ctrl.GetType().Equals(typeof(T)) &&
          (string.IsNullOrEmpty(controlID) || (!string.IsNullOrEmpty(controlID) && ctrl.ID.Equals(controlID))))
        {
            foundControl = ctrl;
        }
        else if (ctrl.Controls.Count > 0)
        {
            foundControl = FindControlRecursive<T>(ctrl, controlID);
        }
        if (foundControl != null)
            break;
    }
    return (T)foundControl;
}

问题在于FindControl对图像无效。