본문 바로가기
Web Program/Asp.net Lecture

Gridview image 동적 추가

by 현이빈이 2009. 8. 26.
반응형


GridView 에 동적으로 Image 를 넣어보자

aspx 코드

<asp:GridView id="GridView1" runat="server" DataSourceID="SqlDataSource2" AutoGenerateColumns="False" AllowPaging="True" BorderWidth="0px" GridLines="Vertical" PageSize="20" OnRowDataBound="GridView1_RowDataBound"
              CellPadding="2" CellSpacing="1" CssClass="table_boxline" Width="100%" OnRowCreated="GridView1_RowCreated">
             
              <Columns>
                <asp:BoundField HeaderText="번호"></asp:BoundField>
                <asp:TemplateField HeaderText="이미지">
                  <ItemTemplate>
                    <asp:Image ID="Image1" runat="server" Width="250" />
                  </ItemTemplate>
                  <ItemStyle Width="250px" />
                </asp:TemplateField>
                <asp:BoundField HeaderText="파일명" InsertVisible="False" ReadOnly="True" DataField="USER_FILE_NAME" />
                <asp:BoundField HeaderText="파일 사이즈" InsertVisible="False" ReadOnly="True" DataField="FILE_SIZE" />
                
                <asp:BoundField HeaderText="path" InsertVisible="False" ReadOnly="True" DataField="SAVE_PATH" />
                <asp:BoundField HeaderText="file" InsertVisible="False" ReadOnly="True" DataField="SAVE_FILE_NAME" />
              </Columns>

              <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
              <PagerStyle CssClass="page_bg" HorizontalAlign="Center" />
              <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
              <HeaderStyle CssClass="txt_boxtle" />
              <AlternatingRowStyle CssClass="table_cell_gray2" />
              <RowStyle CssClass="table_cell_gray1" />
              <PagerSettings NextPageImageUrl="/img/btn_end.gif" PreviousPageImageUrl="/img/btn_before.gif" />
            </asp:GridView>




aspx.cs

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
   int intNo = 0;
   intNo = e.Row.RowIndex + 1;
   e.Row.Cells[0].Text = intNo.ToString();


   string strLink;
   strLink = e.Row.Cells[4].Text.ToString() + "/" + e.Row.Cells[5].Text.ToString();

   if (strLink != "")
   {
    Image image = e.Row.FindControl("Image1") as Image;
    image.ImageUrl = strLink; 
   }
  }
 }
 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
 {
  if (e.Row.RowType == DataControlRowType.Header || e.Row.RowType == DataControlRowType.DataRow)
  {
   e.Row.Cells[4].Visible = false;
   e.Row.Cells[5].Visible = false;
  }
 }


중요한건 서버 이미지 컨트롤을 찾는것일 것이다.
Image image = e.Row.FindControl("Image1") as Image;
반응형