July 22, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Working with Binary Large Objects (BLOBs) Using SQL Server and ADO.NET

  • July 28, 2008
  • By Bipin Joshi
  • Send Email »
  • More Articles »

The Title bound field is data bound with Title property of Photo class. The Description and Photo fields are template fields. The CommnadField adds insert, update and delete abilities to the DetailsView. The markup of these four fields is shown in Listing 6:

Listing 6: Fields of DetailsView

<asp:BoundField DataField="Title" HeaderText="Title :" />
<asp:TemplateField HeaderText="Description :">
   <EditItemTemplate>
      <asp:TextBox ID="TextBox1"
                   runat="server"
                   Columns="40"
                   Rows="5"
                   Text='<%# Bind("Description") %>'
                   TextMode="MultiLine">
      </asp:TextBox>
   </EditItemTemplate>
   <InsertItemTemplate>
      <asp:TextBox ID="TextBox1"
                   runat="server"
                   Columns="40"
                   Rows="5"
                   Text='<%# Bind("Description") %>'
                   TextMode="MultiLine">
      </asp:TextBox>
   </InsertItemTemplate>
   <ItemTemplate>
      <asp:Label ID="Label1"
                 runat="server"
                 Text='<%# Bind("Description")
                 %>'>
      </asp:Label>
   </ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Photo :">
   <EditItemTemplate>
      <asp:FileUpload ID="FileUpload1"
                      runat="server" />
   </EditItemTemplate>
   <InsertItemTemplate>
      <asp:FileUpload ID="FileUpload2"
                      runat="server" />
   </InsertItemTemplate>
   <ItemTemplate>
      <asp:Image ID="Image1"
                 runat="server"
                 ImageUrl='<%# Eval("PhotoID","
                 ~/showphoto.aspx?photoid={0}") %>' />
   </ItemTemplate>
</asp:TemplateField>
<asp:CommandField ButtonType="Button"
                  ShowDeleteButton="True"
                  ShowEditButton="True"
                  ShowInsertButton="True" />

As you can see from Listing 6, the Description template field consists of a multiline textbox in its EditItemTemplate and InsertItemTemplate. This textbox is bound with the Description property of the Photo class. The Photo template field consists of an Image control in its ItemTemplate and FileUpload controls in its EditItemTemplate and InsertItemTemplate. Carefully observe the line marked in bold letters. The ImageUrl property of the Image control is set to ShowPhoto.aspx, a web form you will develop in later sections. Notice how the PhotoID is passed to this web form as a query string parameter using the {0} syntax.

Next, switch to the code behind file of the web form and add a private method (BindData) as shown in Listing 7:

Listing 7: Binding DetailsView with generic List

private void BindData()
{
   List<Photo> photos = PhotoHelper.GetAll();
   DetailsView1.DataSource = photos;
   DetailsView1.DataBind();
}

The BindData() method calls the GetAll() method of the PhotoHelper class. The returned generic list of Photo objects then is bound with the DetailsView. Then, handle the Page_Load event, as shown in Listing 8:

Listing 8: Page_Load event handler of the default web form

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)
   {
      BindData();
      if (DetailsView1.Rows.Count == 0)
      {
         DetailsView1.ChangeMode(DetailsViewMode.Insert);
      }
   }
}

Here, you simply call the BindData() method. If there are no rows to show in the DetailsView, you change its mode to insert. Because the DetailsView is not bound with any data source control, you need to handle its PageIndexChanging and ModeChanging events manually (see Listing 9).

Listing 9: Implementing paging feature for DetailsView

protected void DetailsView1_PageIndexChanging
(object sender, DetailsViewPageEventArgs e)
{
   DetailsView1.PageIndex = e.NewPageIndex;
   BindData();
}
protected void DetailsView1_ModeChanging
(object sender, DetailsViewModeEventArgs e)
{
   DetailsView1.ChangeMode(e.NewMode);
   BindData();
}

The code in Listing 9's event handlers is straightforward and needs no explanation. When a user enters data for a new photo to be stored and clicks on Insert button, the ItemInserting event of the DetailsView is fired. The ItemInserting event hander looks as shown in Listing 10:





Page 4 of 7



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel