I have a page that allows you to insert a new item or update an existing item. I am using a formview for it, I have custom validation in place for the insert to check that the item number isn't already stored in the database, if it is a panel appears informing the user, if it doesnt a panel appears with a success message. I am trying to do the same for the update and i am using the same code but it isn't working. Anyone any ideas?

The code for the insert is:

  protected void fvItem_ItemInserting(object sender, FormViewInsertEventArgs e)
        {
            //record user details
            e.Values["ItemInputByUserId"] = Context.User.Identity.Name;
            e.Values["ItemDateInput"] = System.DateTime.Now;

            UpdatePanelGVItem.Update();
        }

        protected void fvItem_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            if (e.Exception == null)
            {
                UpdatePanelGVItem.Update();
                GVItem.DataBind();
                fvItem.ChangeMode(FormViewMode.ReadOnly);
                ModalPopupExtenderInsert.Hide();

                //set success panel details
                pnlSuccess.Visible = true;
                lblSuccess.Text = "A new item has been successfully created";
                lblSuccessHeading.Text = "Item successfully created";
            }
        }

protected void custValtbItemNum_ServerValidate(object source, ServerValidateEventArgs args)
        {
            TextBox newItem = (TextBox)fvItem.FindControl("ItemNumTextBox");

            //check if item already exists in system
            String ItemNum = newItem.Text;
            ITEM a = (ITEM)ITEMManager.ITEMGetByNum(ItemNum);
            String existItem = a.ItemNum;

            args.IsValid = (ItemNum != existItem);

            if (args.IsValid == true)
            {
                pnlSuccess.Visible = true;
            }
            else
            {
                pnlFail.Visible = true;
            }
        }   

The code for the upadte is

 protected void fvItem_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            UpdatePanelGVItem.Update();
        }

        // when the itemCRM is updated the grid view shows this and the popup closes
        protected void fvItem_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
        {
            if (e.Exception == null)
            {

                UpdatePanelGVItem.Update();
                GVItem.DataBind();
                fvItem.ChangeMode(FormViewMode.ReadOnly);

                //set success panel details
                pnlSuccess.Visible = true;
                lblSuccess.Text = "CRM details have been successfully updated";
                lblSuccessHeading.Text = "CRM successfully updated";
            }   
        }

         protected void CustomValTBItem_ServerValidate(object source, ServerValidateEventArgs args)
        {

         TextBox newItem = (TextBox)fvItem.FindControl("ItemNumTextBox");

            //check if item already exists in system
            String ItemNum = newItem.Text;
            ITEM a = (ITEM)ITEMManager.ITEMGetByNum(ItemNum);
            String existItem = a.ItemNum;

            args.IsValid = (ItemNum != existItem);

            if (args.IsValid == true)
            {
                pnlSuccess.Visible = true;
            }
            else
            {
                pnlFail.Visible = true;
            }
        }
Member Avatar for LastMitch

I am using a formview for it, I have custom validation in place for the insert to check that the item number isn't already stored in the database, if it is a panel appears informing the user, if it doesnt a panel appears with a success message. I am trying to do the same for the update and i am using the same code but it isn't working. Anyone any ideas?

How did you figure it out? Can you share how you did it?

Did you include or add something like this:

protected void fvItem_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
    if (!this.IsValid)
        return;
    // some code here
    try
    {
    //some code here
    UpdatePanelGVItem.Update();
    }
    catch (RuleException exc)
    {
    // some code here
    }
}

I used the same code, It wasn't working because i'd misspelled the text box in the findcontrol. Silly error!

I am using same code but it's working properly.

Have you tried the inbuilt validators within Asp.Net?

I needed a custom validator for what I needed it to do but have got it working now. Thanks

It's good that you have resolved your issue. Congrats.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.