Steven
Steven

Reputation: 19425

Not able to display content in <EditItemTemplate> in FormView

What am I doing wrong since the content in the < EditItemTemplate > is not displayed when I click the Edit button?

<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging">

  <ItemTemplate>
    //display content here
    <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
  </ItemTemplate>

  <EditItemTemplate>
    This text should be displayed when I click the Edit button
    <asp:LinkButton runat="server" ID="UpDateButton" CausesValidation="false" CommandName="Update" Text="Lagre" />
  </EditItemTemplate>            

</asp:FormView>

Update

This is my code-behind:

namespace development.templates
{
    public partial class HotelDetails : TemplatePage
    {
        static Hotel hotel;
        protected DataRow drHotel;
        DataTable dtCriteria;
        DataTable dtHotel;
        private HotelCriteria hotelCriteria;

        protected void Page_Load(object sender, EventArgs e)
        {
            int hotelID = Convert.ToInt32(Request.QueryString["hotelid"].ToString());

            if (!IsPostBack)
            {
                if (hotelID != 0)
                {
                    // Create Hotel instance based on hoteID.
                    hotel = new Hotel(hotelID);
                    drHotel = hotel.hotelData.Rows[0];
                    dtHotel = hotel.getHotelsByCity(drHotel["city"].ToString());


                    // Hotel scrore is calculated from a score which is derived from certain criterias.
                    hotelCriteria = new HotelCriteria(hotelID);
                    dtCriteria = hotelCriteria.getHotelCriteria();

                    //Set datasource for hotel list in right sidebar.
                    hotelListByCity.DataSource = correctList(dtHotel, hotelID);
                    hotelListByCity.DataBind();

                    // Set datasource for current hotel
                    fwHotelDetails.DataSource = hotel.hotelData;
                    fwHotelDetails.DataBind();

                }
            }
        }



        protected void fwHotelDetails_DataBound(object sender, EventArgs e)
        {
            //Find the criteria list and set the datasource
            Repeater rep = (Repeater)fwHotelDetails.FindControl("repCriteriaScore");

            rep.DataSource = this.dtCriteria;
            rep.DataBind();

            // Controll is user is logged in. If logged in, then user may add, edit or delete hotel record.
            System.Security.Principal.IPrincipal user = Context.User;
            if ((user != null) && user.Identity.IsAuthenticated){
                Panel panel = (Panel)fwHotelDetails.FindControl("administrationPanel");
                panel.Visible = true;
            }
        }


        protected void fwHotelDetails_ModeChanging(object sender, FormViewModeEventArgs e)
        {
            switch (e.NewMode)
            {
                case FormViewMode.Edit:
                    MessageLabel.Text = "Edit mode";
                    fwHotelDetails.ChangeMode(FormViewMode.Edit);
                    break;
                case FormViewMode.ReadOnly:
                    MessageLabel.Text = "Read mode";
                    break;
                case FormViewMode.Insert:
                    MessageLabel.Text = "Insert mode";
                    break;
            }
        }
    }
}

Upvotes: 2

Views: 3439

Answers (3)

Danette Cross
Danette Cross

Reputation: 11

The EditItemTemplate also won't show up if the record is empty. In other words, if you have a gridview that you select a detail record from that is feeding the formview, and their is no detail for the selected grid item, then the form won't show up at all. I check to see if the detail record is null, and if so, I set the formview or detailsview to "insert" mode. so they can enter a new record.

Upvotes: 1

SecretDeveloper
SecretDeveloper

Reputation: 3140

Ok have you tried putting a breakpoint on fwHotelDetails_ModeChanging and then debugging the App? Does the breakpoint get hit when you click the edit button.

At least this will tell you where your problem lies. That is [1] the events are not hooked up correctly or [2] there is something going wrong with ChangeMode.

I realise this isnt a solution but if you tell me whether the breakpoint hits i can help you further.

Upvotes: 0

BenB
BenB

Reputation: 10620

In your function fwHotelDetails_ModeChanging, add this:

fwHotelDetails.ChangeMode(FormViewMode.Edit)

i.e.

    Protected Sub fwHotelDetails_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles fwHotelDetails.ModeChanging
        fwHotelDetails.ChangeMode(FormViewMode.Edit)
    End Sub

Upvotes: 0

Related Questions