0

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;
            }
        }
4
Contributors
7
Replies
33
Views
4 Years
Discussion Span
Last Post by annaharris
0

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
    }
}
0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.