Link to home
Start Free TrialLog in
Avatar of vfuwhite
vfuwhite

asked on

Custom validator in edititemtemplate of a datagrid

Hello,

I have an editable datagrid and wish to use a custom vaidator on one of the fields as it needs to check back with the database to determine if the entry is valid. Hence it need to use a server side validation function.

However I cannot get the error message to display. Even if I just put in a test procedure in that sets IsValid = False the error message will not display. It always thinks that the page is valid. A similar custom validator in the footer of the datagrid works fine.

Is it something to do with running a server-side validation?

here is the column

<asp:templatecolumn>                                          
<headertemplate>Unit</headertemplate>
            <headerstyle Font-Bold="true" Width="100"></headerstyle>
      <itemtemplate><%# DataBinder.Eval(Container.DataItem, "UNIT") %></itemtemplate>
            <itemstyle Width="100"></itemstyle>
      <edititemtemplate>
            <asp:dropdownlist runat="server" id="ddlOutputUnit" CssClass="DDL_STYLE" Width="100" DataValueField="UNIT_ID" DataTextField="UNIT" />
            <asp:customvalidator CssClass="ERR_STYLE" id="cvdCompUnitUpdate" runat="server" ErrorMessage="Must use the compulsory unit" ControlToValidate="ddlOutputUnit" Display="Static" OnServerValidate="Test_Validate"/>            
      </edititemtemplate>                              
      <footertemplate>
            <asp:dropdownlist runat="server" id="ddlOutputUnit_add" CssClass="DDL_STYLE" Width="100" DataValueField="UNIT_ID" DataTextField="UNIT"/>
            <asp:customvalidator CssClass="ERR_STYLE" id="cvdCompUnit" runat="server" ErrorMessage="Must use the compulsory unit" ControlToValidate="ddlOutputUnit_add" Display="Dynamic" OnServerValidate="cvdCompUnit_Validate"/>            
      </footertemplate>
</asp:templatecolumn>


cvdCompUnit_Validate works fine

but even if I set

      Private Sub test_Valid(source as object, args as ServerValidateEventArgs)
            args.IsValid = False
      End Sub


the error message is never displayed and the page is always valid

Avatar of Jojo1771
Jojo1771

This is simpe enoguh.  


In your event for update

do this


CType(e.Item.FindControl("cvdCompUnitUpdate"), customvalidator ).IsValid = False

Datagirds have lots of items, so there is no way to call it by ID.

If this not work please post your code behind. So I can take a look.
Avatar of vfuwhite

ASKER

I don't see the point in setting the validator to false in the updatecommand

Anyway below if the update command

the page is definitely not valid but the error message doesn't display and the datagrid returns to the non-edit mode

Sub dgOutputs_Update(sender as Object, e as DataGridCommandEventArgs)
      
      Response.Write("<BR>dgOutputs_Update")
      Page.Validate
      if page.isvalid then
            Response.Write("<BR> Page IS VALID")
            Dim intOutcomeID as int32 = ddloutcome.Selecteditem.Value
            
            Dim intID as Int32 = int32.Parse(dgOutputs.DataKeys(e.Item.ItemIndex))
            Dim strSMU as String = e.Item.Cells(1).text
            Dim intOutputID as int32 = Ctype(e.Item.FindControl("ddlOutput"), DropDownList).SelectedItem.Value
            Dim intWeed as int32
            
            'Only Save the Weed if the Output is Weeding
            if IsWeedingOutput(intOutputID) then
                  intWeed = Ctype(e.Item.FindControl("ddlWeed"), DropDownList).SelectedItem.Value
            end if
      
            Dim dblQty as Double = Ctype(e.Item.FindControl("txtQty"), textbox).text
            Dim intOutputUnitID as int32 = Ctype(e.Item.FindControl("ddlOutputUnit"), DropDownList).SelectedItem.Value
            
            Dim strSQL as string = "UPDATE SMU_LVL2_ACTIVITIES_TBL " & _
                  " SET SMU_LVL2_CODE = @SMU_LVL2_CODE, ACTIVITY_ID = @ACTIVITY_ID, QTY = @QTY, " & _
                  " UNIT_ID = @UNIT_ID, WEED_ID = @WEED_ID " & _
                  " WHERE [SMU_LVL2_ACTIVITY_ID] = " & intID                  
            ConnectDB()
            dim cmd as oledbCommand = New oledbCommand(strSQL, m_objConn)
            cmd.Commandtype = Commandtype.text
            With  cmd
                  .Parameters.add("@SMU_LVL2_CODE",oledbtype.varchar)
                  .Parameters.add("@ACTIVITY_ID",oledbtype.integer)
                  .Parameters.add("@QTY",oledbtype.double)
                  .Parameters.add("@UNIT_ID",oledbtype.integer)
                  .Parameters.add("@WEED_ID",oledbtype.integer)
                  .Parameters("@SMU_LVL2_CODE").value = strSMU
                  .Parameters("@ACTIVITY_ID").value = intOutputID
                  .Parameters("@QTY").value = dblQty
                  .Parameters("@UNIT_ID").value = intOutputUnitID
                  If intWeed = nothing then
                        .Parameters("@WEED_ID").value = DBNull.value
                  else
                        .Parameters("@WEED_ID").value = intWeed                  
                  end if
            End With
            Dim lRecordsAffected = cmd.ExecuteNonQuery()
            DisconnectDB()                  
            dgOutputs.showfooter=false
            dgOutputs.EditItemIndex = -1
            SetUpOutputData()
            SetUpOutputUnitAdd()
                Else
                      Response.Write("<BR> PAGE IS NOT VALID")
               End if
      
End sub      
It seems that if I put the Page.Validate at the start of the Sub then the page is always invalid. If I remove it the page is always valid.
ASKER CERTIFIED SOLUTION
Avatar of Jojo1771
Jojo1771

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am only putting  Response.Write("<BR> PAGE IS NOT VALID") in the code to debug. I don't want the update to do anything if the page is not valid except dgOutputs.EditItemIndex = e.Item.ItemIndex like you have noted above, and display the custom error message of course.

The trouble is that it does not get there even if the custom validator returns false. The page is always valid. However if I put a page.validate in there the page is always invalid and it will display the above error message. However it still does not display the custom error message.

I have also tried using a error message in an asp:label and setting it to visisble if the page the page is invalid but it will also not display.

I have tested the custom validator works with client side validation and it works fine.

I also have a custom validator in the footer of the datagrid and it also works fine.

So I assume it is something that happens in the update command.

The only thng I can get working is a label outside of the datagrid with an an error message. I now have function that runs the valdation check within the Update Command and displays the label error message if it returns false.

The only trouble is that the dgOutputs.EditItemIndex = e.Item.ItemIndex does not leave the record in the editing state. The update does not run because of the validation check but it returns to the non-editing state. Here is the current code :

Sub dgOutputs_Update(sender as Object, e as DataGridCommandEventArgs)

      Dim intOutcomeID as int32 = ddloutcome.Selecteditem.Value
            
               Dim intID as Int32 = int32.Parse(dgOutputs.DataKeys(e.Item.ItemIndex))
      Dim strSMU as String = e.Item.Cells(1).text
      Dim intOutputID as int32 = Ctype(e.Item.FindControl("ddlOutput"), DropDownList).SelectedItem.Value
      Dim intWeed as int32
            
      'Only Save the Weed if the Output is Weeding
      if IsWeedingOutput(intOutputID) then
            intWeed = Ctype(e.Item.FindControl("ddlWeed"), DropDownList).SelectedItem.Value
      end if
      
      Dim dblQty as Double = Ctype(e.Item.FindControl("txtQty"), textbox).text
      Dim intOutputUnitID as int32 = Ctype(e.Item.FindControl("ddlOutputUnit"), DropDownList).SelectedItem.Value
            
      'Check the Compulsory Unit
      If CompUnitUpdate_Validate(intOutcomeID,  intOutputID, intOutputUnitID, strSMU, intID) then
                  
            Dim strSQL as string = "UPDATE SMU_LVL2_ACTIVITIES_TBL " & _
                        " SET SMU_LVL2_CODE = @SMU_LVL2_CODE, ACTIVITY_ID = @ACTIVITY_ID, QTY = @QTY, " & _
                        " UNIT_ID = @UNIT_ID, WEED_ID = @WEED_ID " & _
                              " WHERE [SMU_LVL2_ACTIVITY_ID] = " & intID                  
            Try
                  ConnectDB()
                  dim cmd as oledbCommand = New oledbCommand(strSQL, m_objConn)
                  cmd.Commandtype = Commandtype.text
                  With  cmd
                        .Parameters.add("@SMU_LVL2_CODE",oledbtype.varchar)
                        .Parameters.add("@ACTIVITY_ID",oledbtype.integer)
                        .Parameters.add("@QTY",oledbtype.double)
                        .Parameters.add("@UNIT_ID",oledbtype.integer)
                        .Parameters.add("@WEED_ID",oledbtype.integer)
                        .Parameters("@SMU_LVL2_CODE").value = strSMU
                        .Parameters("@ACTIVITY_ID").value = intOutputID
                        .Parameters("@QTY").value = dblQty
                        .Parameters("@UNIT_ID").value = intOutputUnitID
                        If intWeed = nothing then
                              .Parameters("@WEED_ID").value = DBNull.value
                        else
                              .Parameters("@WEED_ID").value = intWeed                  
                        end if
                  End With
                  Dim lRecordsAffected = cmd.ExecuteNonQuery()
            Catch ex as exception            
                  DisconnectDB()
                  Response.Write(ex.Message)
                  Response.End
            Finally
                           DisconnectDB()                  
            End Try
            dgOutputs.showfooter=false
            dgOutputs.EditItemIndex = -1
            SetUpOutputData()
            SetUpOutputUnitAdd()
      Else
            Response.Write("<BR> <BR> Page IS NOT VALID")
            lblTest.Visible = True
            dgOutputs.EditItemIndex = e.Item.ItemIndex
      End if
      
End sub      
OK I have worked out that I need a dgOutputs.DataBind after the last line and it remains in the Edit Mode.

Still doesn't answer my original question. But it will have to do.

Thanks
Vicky